bertZuo 2018-12-03
本篇文章扣丁学堂小编主要是和大家分享一下canvas仿写贝塞尔曲线的示例,文章中会有代码列出供大家详细了解。对HTML5感兴趣想要学好HTML5开发技术的小伙伴们随着小编一起来看一下吧。
HTML5在线教程
代码如下:
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 = " ";
}
以上就是小编给大家分享的canvas仿写贝塞尔曲线的示例代码,希望对小伙伴们有所帮助,想要了解更多内容的小伙伴可以登录扣丁学堂官网咨询。
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...
一个客户要求给网站添加一个背景音乐,我用的是html5添加网页背景音乐的代码,在此记录一下以后有用。html5方法二:<videocontrols=""autoplay=""name="media&q