Web

JS中canvas画布绘制中的缩放,位移,旋转

2019-09-29  本文已影响0人  追逐_chase
web.jpeg

1.缩放


<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    
    cxt.rect(0,0,100,100);
    cxt.fillStyle = "red";
    //缩放
    cxt.scale(2,2);
    //放大一倍,不仅仅是宽度和高度,其x,y也会跟着放大
    cxt.fillStyle = "blue";
    cxt.rect(100,100,100,100);
    
    //还原 缩小原来的一倍
    cxt.scale(0.5,0.5);
    
    
    cxt.fill();
    
</script>
image.png

2.位移

<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    
    cxt.fillStyle = "red";
    cxt.fillRect(0,0,200,100);
    
    //位移  对之前的操作 没有影响
    cxt.translate(100,100);
    cxt.fillStyle = "blue";
    cxt.fillRect(100,100,200,100);
    
</script>

image.png

3.旋转

<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");

  cxt.fillStyle = "red";
   cxt.fillRect(100,100,200,100);
   
   cxt.rotate(20 * Math.PI/180);
   cxt.fillStyle = "blue";
   cxt.fillRect(100,100,200,100);
   
</script>
image.png

4.状态的保存和还原



<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d"); 
  cxt.fillRect(100,100,100,100);
  //保存状态
  cxt.save();
  
  //第二个状态
  cxt.translate(100,100);
  cxt.rotate(20 *Math.PI/180);
  cxt.fillStyle = "red";
  cxt.fillRect(100,100,200,100);
  
  //第三个状态
  //还原状态
  cxt.restore();
  cxt.fillStyle = "blue";
  cxt.fillRect(200,100,300,100);
</script>
image.png

<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    
    //绘制矩形

    // cxt.fillRect(200,200,200,100);
    var ang = 20;
    setInterval(function(){
        //清屏
        cxt.clearRect(0,0,canvas.clientWidth,canvas.clientHeight);
        //保存状态
        cxt.save();
        cxt.fillStyle = "red";
        //平移
        cxt.translate(300,200);
        //旋转
        cxt.rotate(ang * Math.PI/180);
        //要相对于平移后的坐标点 旋转
        //要移动自身坐标的宽度和高度的一半 才会到相对平移画布的远点
        cxt.fillRect(-100,-50,200,100);
        ang += 10;
        cxt.restore();
    },200);
    
    
    
    
</script>
Untitled.gif

5.将绘制的内容通过Base64转码 输出二进制数据

<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    
    //绘制矩形
    cxt.fillStyle = "red";
    cxt.fillRect(200,200,200,100);
    
    //绘制文字
    cxt.font = "20px '微软雅黑'";
    cxt.strokeText("来来,你是谁",100,100);
    
    //转化为base64
    /*
        canvas.toDataURL(type, encoderOptions);
        type,设置输出的类型,比如 image/png image/jpeg等
        encoderOptions: 0-1之间的数字,用于标识输出图片的质量,1表示无损压缩,类型为: image/jpeg 或者image/webp才起作用。
        
    */
     var data = canvas.toDataURL("image/png",1);
     
     var img = document.getElementById("img_id");
     img.src = data;
    
    
</script>

image.png

6.合成属性globalCompositeOperation

<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    
    //绘制一个矩形
    cxt.fillStyle = "red";
    cxt.fillRect(200,200,200,100);
    
    //合成属性
    cxt.globalCompositeOperation = "source-over";
    
    //绘制一个圆形
    cxt.fillStyle = "#008000";
    cxt.arc(400,300,100,0,360*Math.PI/180);
    cxt.fill();
    
    
</script>
image.png image.png image.png image.png image.png image.png image.png image.png image.png
一个刮刮乐的例子
<script type="text/javascript">
    var canvas = document.getElementById("con");
    //获取上下文
    var cxt = canvas.getContext("2d");
    cxt.fillStyle = "#ccc";
    cxt.fillRect(0,0,320,160);
    //合成属性
    cxt.globalCompositeOperation = "destination-out";
    canvas.onmousedown = function(){
        
        canvas.onmousemove = function(e){
            //绘制圆形
            cxt.arc(e.offsetX,e.offsetY,20,0,2 * Math.PI);
            //渲染
            cxt.fill();
        }
        
        canvas.onmouseup = function(){
            canvas.onmousemove = null;
            canvas.onmouseup = null;
        }
    }
    
    
</script>
Untitled.gif
上一篇下一篇

猜你喜欢

热点阅读