南阳建网站企业有哪些,个人网站备案备注,卡姿兰网站建设策划书,wordpress第三方一、场景描述
利用js中的canvas画图来画图#xff0c;爱心、动画。
二、问题拆解
第一个是#xff1a;canvas画图相关知识。 第二个是#xff1a;动画相关内容。
三、知识背景
3.1 canvas画图相关内容
canvas画图的基本步骤
获取页面上的canvas标签对象获取绘图上下文…一、场景描述
利用js中的canvas画图来画图爱心、动画。
二、问题拆解
第一个是canvas画图相关知识。 第二个是动画相关内容。
三、知识背景
3.1 canvas画图相关内容
canvas画图的基本步骤
获取页面上的canvas标签对象获取绘图上下文 canvas.getContext(“2d)绘图绘制已定义的路径 strock()
3.2 requestAnimationFrame(callback)方法 替代setTimeout和setInterval来设置动画
requestAnimationFrame这个方法的作用是 回掉函数的调用频率与显示器的刷新率一样。 用法如下所示
const zero performance.now(); //获取当前时间戳function animate() {//利用现在时间戳减去程序开始时的时间戳进行动画制作求余再除 1 秒周期的动画const value ((performance.now() - zero) % 1000) / 1000;//清除当前区域的以画图形ctx.clearRect(0,0,width,height);//重新画图draw(10010*value);console.log(value);//进行回调animationFrame requestAnimationFrame(animate);
}四、场景实现
画爱心 爱心的极坐标公式 x(t) 16* sin^{3}(t) y(t) 13cos(t) - 5cos(2t) - 2cos(3t) - cos(4t)
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/titlestylebody{background-color: black;}/style
/head
bodycanvas idcanvas/canvas
/body
script//根据公式获取爱心图形路径上的点function getPointsXY(){var points [];var x0,y0;for(var i 0;iMath.PI*2;ii0.01){x 16 * Math.pow(Math.sin(i),3)*20;y -(13 * Math.cos(i) - 5 * Math.cos(2*i) - 2 * Math.cos(3*i) - Math.cos(4*i))*20;points.push({x: canvas.width / 2 x,y: canvas.height / 2 y})}return points;}function init() {const width window.innerWidth;const height window.innerHeight;canvas.width width;canvas.height height;}//获取页面上的canvas标签const canvas document.getElementById(canvas);init(); //获取绘图上下文const ctx canvas.getContext(2d);//设置画笔颜色ctx.strokeStyle pink;ctx.lineWidth 5;//开始路径ctx.beginPath();//获取路径上的各个节点var points getPointsXY();//依次连接各个点ctx.moveTo(points[0].x,points[0].y);for(let item of points){ctx.lineTo(item.x,item.y);ctx.moveTo(item.x,item.y);}ctx.lineTo(points[0].x,points[0].y);ctx.moveTo(0,0);// for(let item of points){// ctx.lineTo(item.x,item.y);// ctx.moveTo(item.x,item.y);// }ctx.lineTo(50,50);//闭合路径//ctx.closePath();//描述ctx.stroke();//ctx.fill();
/script
/html图形动画
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleDocument/title
/head
bodycanvas idcanvas/canvas
/body
scriptfunction draw(r){//圆心位置 (x, y), 半径, 起始角度, 结束角度, 方向 (true为顺时针, false为逆时针)ctx.beginPath();ctx.arc(width/2,height/2,r,0,Math.PI*2,false);let grad ctx.createRadialGradient(width/2,height/2, 50, width/2,height/2, 200)grad.addColorStop(0, pink); // 设置渐变颜色grad.addColorStop(0.5, yellow);grad.addColorStop(1, lightblue);ctx.fillStyle grad; // 设置fillStyle为当前的渐变对象ctx.fill();//闭合路径ctx.closePath();//描述ctx.stroke();ctx.beginPath();ctx.arc(width/2,height/2,5,0,Math.PI*2,false)//设置填充色ctx.fillStyle lightblue;//填充图ctx.fill(); }function animate() {const value ((performance.now() - zero) % 1000) / 1000;ctx.clearRect(0,0,width,height);draw(10010*value);console.log(value);animationFrame requestAnimationFrame(animate);}const canvas document.getElementById(canvas);//设置canvas画板大小const width window.innerWidth;const height window.innerHeight;canvas.width width;canvas.height height;const ctx canvas.getContext(2d);let animationFrame null;//开始路径ctx.beginPath();const zero performance.now();console.log(zero);requestAnimationFrame(animate);//draw(100)//闭合路径ctx.closePath();//描述ctx.stroke();
/script
/html