天正在等烟雨,而我在等你,啦啦啦,欢迎关注我的简书,今天分享的是原创的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();
//画蓝球
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 = " ";
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“canvas仿写贝塞尔曲线的示例代码”评论...
更新动态
2025年10月29日
2025年10月29日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
