HTML5 Canvas ( 图形的透明度和遮盖 ) globalAlpha, globalCompositeOperation

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>canvas</title>
    <script type="text/javascript" src="../js/jQuery.js"></script>
    <style type="text/css">
        #canvas{
            width: 7rem;
            margin: .25rem 0 0 1.5rem;
            border: 1px solid black;
            display: block;
        }
    </style>
</head>
<body> 
    <canvas id="canvas" width="1000" height="600"></canvas>
    <button>source-over</button>
    <button>source-atop</button>
    <button>source-in</button>
    <button>source-out</button>
    <button>destination-over</button>
    <button>destination-atop</button>
    <button>destination-in</button>
    <button>destination-out</button>
    <button>lighter</button>
    <button>copy</button>
    <button>xor</button>
</body>
</html>
<script type="text/javascript">
    /**
     * rem 布局初始化
     */
    $('html').css('font-size', $(window).width()/10);
    /**
     * 获取 canvas 画布
     * 获取 canvas 绘图上下文环境
     */
    var canvas = $('#canvas')[0];
    var cxt = canvas.getContext('2d');
    
    /**
     * globalCompositeOperation: 图形的折叠方式
     */
    $('button').click(function(){
        cxt.clearRect(0, 0, canvas.width, canvas.height);
        cxt.globalCompositeOperation = $(this).html();
        cxt.fillStyle = 'red';
        cxt.fillRect(100, 100, 400, 200);
        cxt.fillStyle = 'blue';
        cxt.fillRect(300, 200, 400, 200);
    });
</script>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>canvas</title>
    <script type="text/javascript" src="../js/jQuery.js"></script>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
            outline: none;
            border: none;
        }
        #canvas{
            width: 7rem;
            margin: .25rem 0 0 1.5rem;
            border: 1px solid black;
        }
    </style>
</head>
<body> 
    <canvas id="canvas" width="1000" height="600"></canvas>
</body>
</html>
<script type="text/javascript">
    /**
     * rem 布局初始化
     */
    $('html').css('font-size', $(window).width()/10);
    /**
     * 获取 canvas 画布
     * 获取 canvas 绘图上下文环境
     */
    var canvas = $('#canvas')[0];
    var cxt = canvas.getContext('2d');
    
    /**
     * globalAlpha: 透明度
     */
    for(var i = 0; i < 100; i++){
        var R = Math.floor(Math.random()*225);
        var G = Math.floor(Math.random()*225);
        var B = Math.floor(Math.random()*225);
        var x = R + "," + G + "," + B;
        cxt.fillStyle = "rgb("+x+")";
        cxt.globalAlpha = 0.7;
        cxt.beginPath();
        cxt.arc(Math.random()*canvas.width, Math.random()*canvas.height, Math.random()*70, 0, Math.PI*2);
        cxt.fill();
    }    
</script>

 

转载于:https://www.cnblogs.com/lovling/p/6649025.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值