这篇文章给大家分享的是有关canvas如何仿写贝塞尔曲线的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
成都创新互联公司-专业网站定制、快速模板网站建设、高性价比克井网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式克井网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖克井地区。费用合理售后完善,十余年实体公司更值得信赖。效果图:
html
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(); //画蓝球 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(); } //拖动小球做动画 //判断是否拖动小球 //如果在小球上就做动画 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("鼠标在红球上"); 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(); } } //判断鼠标是否在蓝球上 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 = " "; }
感谢各位的阅读!关于“canvas如何仿写贝塞尔曲线”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!