使用canvas能画各种各样的东西

原文链接: https://www.mk2048.com/blog/blog.php?id=h0j0k2ijibaa&title=%E4%BD%BF%E7%94%A8canvas%E8%83%BD%E7%94%BB%E5%90%84%E7%A7%8D%E5%90%84%E6%A0%B7%E7%9A%84%E4%B8%9C%E8%A5%BF

用过canvas的人都知道,在这个画布上面可以制作各种各样的动画效果,想必大家都用过这个。

晒晒刚刚用这个做的一个demo:

现在来画一个圆看看:

demo.js:

var can,ctx,count = 1,w,h,i;

can = document.getElementById('can');
ctx = can.getContext('2d');

w = document.body.scrollWidth;
h = document.body.scrollHeight;
can.setAttribute('width',w)
can.setAttribute('height',h)
 // angle for   
    angle = Math.PI/2 * count;
    x = w/2   Math.sin(angle);
    y = h/2   Math.sin(angle);
    ctx.beginPath();
    
    ctx.arc(x,y ,h/6,0,2*Math.PI);
    ctx.fillStyle = '#3ff';
    ctx.strokeStyle = '#333';
    ctx.stroke();
    ctx.fill();

对应的.html:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <title>demo</title>
  </head>
  <body bgColor='#000'>
    <canvas id="can"></canvas>
    <script src="js/demo.js"></script>
  </body>
</html>

这个太单调了,我们可以把angle这段代码循环一下,看看demo是什么效果?

demo.js:

for( i = 0; i <count;i  ){
    angle = Math.random(Math.PI/2 * i);
    x = (w/3)*Math.sin(angle);
    y = h/3   (1   angle)*Math.sin(angle);
    ctx.beginPath();
    ctx.arc(2*x,y,h/8,0,2*Math.PI);
    ctx.fillStyle = '#3ff';
    ctx.strokeStyle = '#000';
    ctx.stroke();
    ctx.fill();
}

 不想那么单调放水平,也可以这样有弧度:

有了它以后想做什么(神马)都可以! -/-


更多专业前端知识,请上 【猿2048】www.mk2048.com

猜你喜欢

转载自blog.csdn.net/qq_29069777/article/details/102748563