美文网首页
HTML5 Canvas笔记——图像合成

HTML5 Canvas笔记——图像合成

作者: 没昔 | 来源:发表于2020-05-20 00:02 被阅读0次

    编程展示各种图像的合成效果,效果的名称

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>图像合成</title>
        <style>
            #canvas {
                border: 1px solid cornflowerblue;
                position: absolute;
                left: 150px;
                top: 10px;
                background: #eeeeee;
                border: thin solid #aaaaaa;
                cursor: pointer;
                -webkit-box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
                -moz-box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
                box-shadow: rgba(200, 200, 255, 0.9) 5px 5px 10px;
            }
        </style>
    </head>
    <body>
    <select size="11" id="compositingSelect">
        <option value="source-atop">目标图顶部显源图</option>
        <option value='source-in'>目标图中显源图</option>
        <option value='source-out'>目标图外显源图</option>
        <option value='source-over'>目标图上显源图</option>
        <option value='destination-atop'>源图顶部显目标图</option>
        <option value='destination-in'>源图中显目标图</option>
        <option value='destination-out'>源图外显目标图</option>
        <option value='destination-over'>源图上显目标图</option>
        <option value='lighter'>源图+目标图</option>
        <option value='copy'>只显示源图</option>
        <option value='xor'>异或组合</option>
    </select>
    <canvas id="canvas" width="600" height="420">Canvas not supports</canvas>
    </body>
    <script>
        'use strict';
        let context = document.getElementById('canvas').getContext('2d'),
            selectElement = document.getElementById('compositingSelect');
        /**
         * 绘制文字
         */
        let drawText = () => {
            context.save();
    
            context.shadowColor = 'rgba(100, 100, 150, 0.8)';
            context.shadowOffsetX = 5;
            context.shadowOffsetY = 5;
            context.shadowBlur = 10;
    
            context.fillStyle = 'cornflowerblue';
            context.fillText('HTML', 20, 250);
    
            context.strokeStyle = 'yellow';
            context.strokeText('HTML', 20, 250);
    
    
            context.restore();
        };
    
        //Event handlers……
    
        let windowToCanvas = (canvas, x, y) => {
            let bbox = canvas.getBoundingClientRect();
            return {
              x:x-bbox.left*(canvas.width/bbox.width),
              y:y-bbox.top*(canvas.height/bbox.height)
            };
    
        };
    
        context.canvas.onmousemove=e=>{
            let loc = windowToCanvas(context.canvas,e.clientX,e.clientY);
            context.clearRect(0,0,context.canvas.width,context.canvas.height);
            drawText();
    
            context.save();
    
            //设置选项即为全局合成属性
            context.globalCompositeOperation=selectElement.value;
            context.beginPath();
            context.arc(loc.x,loc.y,100,0,Math.PI*2,false);
            context.fillStyle='orange';
            context.stroke();
            context.fill();
    
            context.restore();
        }
    
        //Init……
    
        selectElement.selectedIndex=3;
        context.lineWidth=0.5;
        context.font='128pt Comic-sans';
        drawText();
    </script>
    </html>
    

    效果如图:


    相关文章

      网友评论

          本文标题:HTML5 Canvas笔记——图像合成

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