簡單描述:頁面上有四個點(diǎn),鼠標(biāo)拖動四個點(diǎn)的位置來改變貝塞爾曲線的形狀,雙擊放置點(diǎn)位
創(chuàng)新互聯(lián)從2013年成立,先為呼中等服務(wù)建站,呼中等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為呼中企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
效果圖:
代碼:
Title be12
js:
/** * Created by Administrator on 2017/8/11. * js/max.js */ (function () { var curEle = null; var startPointView = document.querySelector(".startPoint"); var endPointPointView = document.querySelector(".endPoint"); var point1View = document.querySelector(".point1"); var point2View = document.querySelector(".point2"); var context = null; function init() { var canvasEle = document.querySelector("#box"); canvasEle.width = innerWidth; canvasEle.height = innerHeight; //實(shí)時監(jiān)聽網(wǎng)頁大小 window.onresize = function () { canvasEle.width = innerWidth; canvasEle.height = innerHeight; }; context = canvasEle.getContext("2d"); context.strokeStyle = "white"; context.lineWidth = 10; //貝塞爾曲線簡單用法 context.beginPath(); context.moveTo(300,300); context.bezierCurveTo(500,200,600,250,600,600); context.stroke(); //循環(huán)獲取四個點(diǎn)的數(shù)組 for(var i=0;i<4;i++){ addEvent([startPointView,endPointPointView,point1View,point2View][i]); } //鼠標(biāo)雙擊移除鼠標(biāo)滑動事件 放下拖動的點(diǎn) document.ondblclick = function () { document.removeEventListener("mousemove",move); }; } //鼠標(biāo)按下拖動 function addEvent(ele) { ele.onmousedown = function () { curEle = this; document.addEventListener("mousemove",move); }; } //獲取拖動位置并繪制貝塞爾曲線 function move(event) { curEle.style.left = event.pageX+"px"; curEle.style.top = event.pageY+"px"; context.clearRect(0,0,innerWidth,innerHeight); context.beginPath(); context.moveTo(getLeft(startPointView),getTop(startPointView)); context.bezierCurveTo(getLeft(point1View),getTop(point1View),getLeft(point2View),getTop(point2View),getLeft(endPointPointView),getTop(endPointPointView)); context.stroke(); } function getLeft(ele) { return parseInt(ele.style.left); } function getTop(ele) { return parseInt(ele.style.top); } init(); })();
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。