zoukankan      html  css  js  c++  java
  • html5 canvas画布上合成

    source-over 默认。在目标图像上显示源图像。
    source-atop 在目标图像顶部显示源图像。源图像位于目标图像之外的部分是不可见的。
    source-in 在目标图像中显示源图像。只有目标图像内的源图像部分会显示,目标图像是透明的。
    source-out 在目标图像之外显示源图像。只会显示目标图像之外源图像部分,目标图像是透明的。
    destination-over 在源图像上方显示目标图像。
    destination-atop 在源图像顶部显示目标图像。源图像之外的目标图像部分不会被显示。
    destination-in 在源图像中显示目标图像。只有源图像内的目标图像部分会被显示,源图像是透明的。
    destination-out 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。
    lighter 显示源图像 + 目标图像。
    copy 显示源图像。忽略目标图像。
    source-over 使用异或操作对源图像与目标图像进行组合。

    以上是HTML 5 canvas globalCompositeOperation 属性

    <!DOCTYPE html>
    <html>
    <body>
    
    <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>
    
    <script>
    
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.fillStyle="red";
    ctx.fillRect(20,20,75,50);
    ctx.fillStyle="blue";	
    ctx.globalCompositeOperation="source-over";
    ctx.fillRect(50,50,75,50);	
    ctx.fillStyle="red";
    ctx.fillRect(150,20,75,50);
    ctx.fillStyle="blue";	
    ctx.globalCompositeOperation="destination-over";
    ctx.fillRect(180,50,75,50);	
    
    </script>
    
    </body>
    </html>
    

    HTML 5 canvas globalAlpha 属性    globalAlpha=0-1;

    <!DOCTYPE html>
    <html>
    <body>
    
    <canvas id="myCanvas" width="300" height="150" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.
    </canvas>
    
    <script>
    
    var c=document.getElementById("myCanvas");
    var ctx=c.getContext("2d");
    ctx.fillStyle="red";
    ctx.fillRect(20,20,75,50);
    
    //Turn transparency on
    ctx.globalAlpha=0.2;
    ctx.fillStyle="green"; 
    ctx.fillRect(50,50,75,50); 
    ctx.fillStyle="blue"; 
    ctx.fillRect(80,80,75,50);
    </script>
    
    </body>
    </html>
    

     以上2段为W3SCHOOL上的说明:昨天我写到了,API经过国外翻译之后可能翻译的结果让人感觉无语 所以多实践 才能提高

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>canvas合成</title>
    <script src="js/modernizr.js"></script>
    </head>
    
    <body>
    <script type="text/javascript">
    window.addEventListener('load',eventWindowLoaded,false);
    function eventWindowLoaded(){
        canvasApp();
    }
    function canvasSupport(){
        return Modernizr.canvas;
    }
    function canvasApp(){
        if(!canvasSupport()){
            return;
        }else{
            var theCanvas = document.getElementById('canvas')
            var context = theCanvas.getContext("2d")
    
        }
        drawScreen();
        function drawScreen(){
            //在屏幕上绘制一个大方块
            context.fillStyle = "black";
            context.fillRect(10,10,200,200);
            //保留globalCompositeOperation原有值不变
            //现在绘制一个红色正方形
            
            context.fillStyle = "#ff0000";
            context.fillRect(1,1,50,50);
            
            //现在设置为source-over
            context.globalCompositeOperation = "source-over";
            context.fillRect(60,1,50,50);
            //现在设置为destination-over
            context.globalCompositeOperation = "destination-over";
            context.fillRect(1,60,50,50);
            //现在设置globalAlpha
            context.globalAlpha = .5;
            //现在设置source-atop
            context.globalCompositeOperation = "source-atop";
            context.fillRect(60,60,50,50);
            
            
         }
        
    }
    
    
    </script>
    <canvas id="canvas" width="500" height="500">
    你的浏览器无法使用canvas
    如有疑问加QQ:1035417613;小白童鞋;你的支持是我最大的快乐!!
    </canvas>
    </body>
    </html>
  • 相关阅读:
    全选、全不选、反选
    IE Tester 怎样使用firebug 调试工具?
    策略模式
    模板方法模式
    迭代器模式——android中使用
    android 实现圆形波纹动画
    android 窗口的使用
    AlertDialog 基本使用
    国外android 网站
    ViewDragHelper 任意拖动
  • 原文地址:https://www.cnblogs.com/LoveOrHate/p/4419250.html
Copyright © 2011-2022 走看看