国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > HTML5 > 正文

canvas仿寫貝塞爾曲線的示例代碼

2024-09-05 07:22:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

天正在等煙雨,而我在等你,啦啦啦,歡迎關(guān)注我的簡(jiǎn)書,今天分享的是原創(chuàng)的canvas仿寫貝塞爾曲線方法。具體如下:

效果圖:

html

<canvas id="mycanvas" width="500" height="500">您的瀏覽器不支持canvas</canvas>

css

canvas{ border: 1px solid black;}

js

var canvas = document.getElementById("mycanvas");        var context = canvas.getContext("2d");        var x1 = 100;        var y1 = 100;        var x2 = 400;        var y2 = 400;        draw();        function draw(){            //畫半透明的線            context.beginPath();            context.moveTo(500,0);            context.lineTo(0,500);            context.strokeStyle = "rgba(0,0,0,0.3)";            context.lineWidth = 10;            context.stroke();            //畫連接線            context.beginPath();            context.moveTo(0,500);            context.lineTo(x1,y1);            context.lineWidth = 2;            context.strokeStyle = "black";            context.stroke();            context.beginPath();            context.moveTo(500,0);            context.lineTo(x2,y2);            context.lineWidth = 2;            context.strokeStyle = "black";            context.stroke();            //畫紅球            context.beginPath();            context.arc(x1,y1,10,0,Math.PI*2);            context.fillStyle = "orange";            context.fill();            //畫藍(lán)球            context.beginPath();            context.arc(x2,y2,10,0,Math.PI*2);            context.fillStyle = "blue";            context.fill();            //畫貝塞爾曲線            context.beginPath();            context.moveTo(0,500);            context.bezierCurveTo(x1,y1,x2,y2,500,0);            context.lineWidth = 5;            context.stroke();        }        //拖動(dòng)小球做動(dòng)畫        //判斷是否拖動(dòng)小球        //如果在小球上就做動(dòng)畫        canvas.onmousedown = function(e){            var ev = e || window.event;            var x = ev.offsetX;            var y = ev.offsetY;            //判斷是否在紅球上            var dis = Math.pow((x-x1),2) + Math.pow((y-y1),2);            if(dis<100){                console.log("鼠標(biāo)在紅球上");                canvas.onmousemove = function(e){                    var ev = e || window.event;                    var xx = ev.offsetX;                    var yy = ev.offsetY;                    //清除畫布                    context.clearRect(0,0,canvas.width,canvas.height);                    x1 = xx;                    y1 = yy;                    //重繪制                    draw();                }            }            //判斷鼠標(biāo)是否在藍(lán)球上            var dis = Math.pow((x-x2),2) + Math.pow((y-y2),2);            if(dis<100){                canvas.onmousemove = function(e){                    var ev = e || window.event;                    var xx1 = ev.offsetX;                    var yy1 = ev.offsetY;                    //清除畫布                    context.clearRect(0,0,canvas.width,canvas.height);                    x2 = xx1;                    y2 = yy1;                    //重繪制                    draw();                }            }        }        document.onmouseup =function(){            canvas.onmousemove = " ";        }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宜阳县| 潜江市| 涞源县| 会昌县| 四川省| 鹤峰县| 海晏县| 永定县| 乐至县| 称多县| 文登市| 会理县| 嘉黎县| 大兴区| 霍州市| 财经| 兴海县| 太康县| 金溪县| 宁安市| 江口县| 鹰潭市| 泸定县| 香河县| 惠东县| 正宁县| 普洱| 沈阳市| 安西县| 普宁市| 德兴市| 西乌珠穆沁旗| 黑河市| 台东县| 枣强县| 湖北省| 乌鲁木齐市| 庆阳市| 庆阳市| 金川县| 平舆县|