zoukankan      html  css  js  c++  java
  • js面向对象的封装方法,【案例】

    封装方法:

    /**
     * @矩形canvas库
     * @authors Shimily (275766400@qq.com)
     * @date    2016-12-28 10:30:51
     * @version $Id$
     */
    
    function Rect( options){
        this._init(options);   //执行方法
    }
    Rect.prototype={   
        _init:function(options){
            this.x=options.x || 0;   //参数设置,如果不传参数,则设置默认值
            this.y=options.y || 0;this.opacity=options.opacity===0 ? 0: options.opacity || 1;
            this.scaleX=options.scaleX ||1;
            this.scaleY=options.scaleY ||1;
    
            this.strokeStyle=options.strokeStyle || 'red';
            this.fillStyle=options.fillStyle||'red';
        },
        render:function(ctx){   //执行绘制
            ctx.save();   //先保存状态
            ctx.beginPath();
    
            ctx.translate(this.x, this.y);
            
            ctx.rotate(this.rotation * Math.PI /180);
            ctx.globalAlpha=this.optacity;
            ctx.scale(this.scaleX, this.scaleY);
            //ctx.rect(this.x, this.y, this.w, this.h); //绘制矩形
            ctx.rect(0, 0, this.w, this.h); //绘制矩形   设置旋转为矩形的左顶点,要将画布进行位移ctx.translate(this.x, this.y);
    
            ctx.fillStyle=this.fillStyle;
            ctx.fill();  //填充颜色
    
            ctx.strokeStyle=this.strokeStyle;
            ctx.stroke();
            ctx.restore();   //释放状态
        }
    }

    调用方法:

    var rect= new Rect({   //设置属性
                x:300,
                y:200,
                w:100,
                h:120,
                rotation:30,
                opacity:0.3,
                scaleX:1.5,
                scaleY:1.5,
                fillStyle:'blue',
                strokeStyle:'yellow'
            });
     rect.render(ctx);  //执行
  • 相关阅读:
    双缓冲法解决重绘和闪屏问题
    VC设置视图背景颜色方法
    C++ map,set内部数据结构
    红黑树、平衡二叉树
    堆和栈的区别
    C/C++查找一定范围内的素数(筛法)
    动态内存的传递
    TCP三次握手连接
    php分享十五:php的数据库操作
    php分享十四:php接口编写
  • 原文地址:https://www.cnblogs.com/shimily/p/6240016.html
Copyright © 2011-2022 走看看