美文网首页
HTML5 Canvas随机圆周运动

HTML5 Canvas随机圆周运动

作者: McRay | 来源:发表于2017-03-21 17:08 被阅读0次

圆周运动的原理

圆周运动1.PNG
如上图所示,其实圆周运动,就是通过运动的角度deg来求出物体在x轴和y轴上运动的偏移量,分别为radius*Math.cos(deg)radius*Math.sin(deg)

了解了上述原理之后,就可以开始写一个随机圆周运动的DEMO,效果图如下

圆周运动.PNG

基本的设计思路

*利用面向对象的思想创建一个圆模型对象。
*再创建一个帧动画模型。
*创建实例,执行动画。

创建圆模型对象

将画圆形的x,y坐标、半径、初始角度、颜色、宽度等要素,封装在圆模型这个对象的构造函数里面,然后通过给原型添加方法,一个是draw画圆形的方法和move圆形运动的方法,让所有圆形的实例共享这些方法,还有自己相应的属性,这里用到了,构造函数模式和原型模式创建对象的方法思想。

var Square = function(x,y,radiu,option){
        this.wid = canvas.width;
        this.hei = canvas.height;
        this.ctx = ctx;
        this.x = x;
        this.y = y;
        this.radiu = radiu;
        this.option = option;
        this.radius = Math.random()*5 + 1;
        this.angle = 0;//圆周运动的初始角度
    };
    Square.prototype = {
        draw:function(){
            this.ctx.beginPath();
            this.ctx.strokeStyle = this.option.strokeStyle;
            this.ctx.lineWidth = this.option.lineWidth;
            this.ctx.arc(this.x,this.y,this.radiu,0,2*Math.PI,true);
            this.ctx.stroke();
        },
        move:function(){
            /*根据角度计算出x轴和y轴的偏移量*/
            this.x += this.radius*Math.cos(this.angle*(Math.PI/180));
            this.y += this.radius*Math.sin(this.angle*(Math.PI/180));
            this.angle+=5;//角度每次递增5个像素
            this.draw();
        }
    };

创建帧动画对象

同样的道理创建帧动画对象,并且增加一个存放圆实例的数组,每实例化一个圆形,就将它存到数组中,然后每次render渲染之前,都先把之前的画布清除掉,然后执行start方法循环地渲染画布,达到动画的效果。

var Far = function(){
        this.width = canvas.width;
        this.height = canvas.height;
        this.ctx = ctx;
        this.timer = null;
        this.squares = [];//创建数组,用于存放圆实例
    };
    Far.prototype = {
        //循环渲染
        start : function(){
            this.timer = setInterval((function(param){
                return function(){param.render();}
            })(this),30);
        },
        //渲染方法
        render : function(){
            /*将之前的画布清除掉*/
            this.ctx.clearRect(0,0,canvas.width,canvas.height);
            /*遍历每个圆实例,让这些圆角度发生变化,从而让运动轨迹发送变化*/
            for(i in this.squares){
                this.squares[i].move();
                /*圆角度增加判断*/
                if(this.squares[i].angle>360){
                    this.squares[i].angle = 0;
                }
            }
        }
    };

最后创建实例完成动画效果

/*创建帧实例*/
    var frame = new Far();
    /*创建圆实例*/
    for(var i=0;i<500;i++){
        /*圆的所有属性都是一定范围内的随机数*/
        var x = Math.random()*(canvas.width);
        var y = Math.random()*(canvas.height);
        var radiu = Math.random()*20;
        var r = Math.floor(Math.random()*256);
        var g = Math.floor(Math.random()*256);
        var b = Math.floor(Math.random()*256);
        var a = Math.random();
        var option = {
            strokeStyle : 'rgba('+r+','+g+','+b+','+a+')',
            lineWidth : Math.random()*10
        };
        //把圆实例放到帧模型的数组中
        frame.squares[i] = new Square(x,y,radiu,option);
    }
    //开始渲染
    frame.start();

完整代码地址:https://github.com/McRayFE/CanvasCircle

相关文章

  • HTML5 Canvas随机圆周运动

    圆周运动的原理 如上图所示,其实圆周运动,就是通过运动的角度deg来求出物体在x轴和y轴上运动的偏移量,分别为ra...

  • HTML5 Canvas笔记——在canvas中显示HTML控件

    参考书目:《HTML5 Canvas核心技术 图形、动画与游戏开发》 在canvas中显示HTML控件—随机跳动的...

  • html5 Canvas画图5:曲线之arc

    本文属于《html5 Canvas画图系列教程》 在《html5 Canvas画图教程2:Canvas画线条 基础...

  • canvas笔记

    一:canvas简介 1.1什么是canvas? ①:canvas是HTML5提供的一种新标签 ②:HTML5 ...

  • canvas

    @(HTML5)[canvas与SVG] [TOC] 十 、canvas canvas的基本用法 canvas是H...

  • HTML5 Canvas 完整测试 - canvas 标签

    在 html5 文档内创建 canvas 画布: “画布”(canvas) 是 html5 中独有的元素,通过它可...

  • HTML5 Canvas笔记——绘图剪纸

    参考书目:《HTML5 Canvas核心技术 图形、动画与游戏开发》 HTML5 Canvas绘图剪纸

  • canvas浅尝

    简单了解canvas 1.什么是canvas HTML5 的 canvas 元素使用 JavaScript 在网页...

  • canvas学习一

    一、什么是canvas? canvas 是 HTML5 的标签元素,使用 JavaScript 在canvas里绘...

  • 14-JS canvas 学习

    Canvas简介 canvas作用: canvas元素可以让用户在网页上绘制图形; canvas介绍 HTML5中...

网友评论

      本文标题:HTML5 Canvas随机圆周运动

      本文链接:https://www.haomeiwen.com/subject/dpygnttx.html