canvas-圖像重疊效果

圖像重疊
globalCompositeOperation : 繪制的圖像,在重疊的時(shí)候所產(chǎn)生的效果(復(fù)合操作)

globalCompositeOperation = ‘source-over’ 默認(rèn) 后覆蓋前
globalCompositeOperation = 'destination-over' 前覆蓋后

屬性值 描述
source-over 新圖形會覆蓋原有內(nèi)容之上
source-atop 新圖形中與原有內(nèi)容重疊的部分會被繪制,并覆蓋于原來內(nèi)容之上
source-in 新圖形只會出現(xiàn)原有內(nèi)容重疊的部分,其他區(qū)域變成透明色
source-out 只有新圖形中與原有的內(nèi)容不重疊的部分被保留
destination-over 會在原有內(nèi)容之下繪制新圖形
destination-atop 原有內(nèi)容中與新內(nèi)容重疊的部分會被保留,并會在原有內(nèi)容之下繪制新圖形
destination-in 原有內(nèi)容中與新圖形重疊的部分會被保留,其它區(qū)域都變成透明的
destination-out : 原有內(nèi)容中與新圖形不重疊的部分會被保留。
lighter 兩圖形中重疊部分作加色處理。
copy 只有新圖形會被保留,其它都被清除掉
xor 重疊的部分會變成透明
darker 兩圖形中重疊的部分作減色處理
operation.gif

代碼
HTML

<div id="types">
        <span  class="active">source-over</span>
        <span>source-atop</span>
        <span>source-in</span>
        <span>source-out</span>
        <hr>
        <span>destination-over</span>
        <span>destination-atop</span>
        <span>destination-in</span>
        <span>destination-out</span>
        <hr>
        <span>lighter</span>
        <span>copy</span>
        <span>xor</span>
        <span>darker</span>

    </div>
    <div>
        <pre>
            // source-over: 新圖形會覆蓋原有內(nèi)容之上
            // source-atop: 新圖形中與原有內(nèi)容重疊的部分會被繪制,并覆蓋于原來內(nèi)容之上
            // source-in : 新圖形只會出現(xiàn)原有內(nèi)容重疊的部分,其他區(qū)域變成透明色
            // source-out : 只有新圖形中與原有的內(nèi)容不重疊的部分被保留

            // destination-over : 會在原有內(nèi)容之下繪制新圖形
            // destination-atop: 原有內(nèi)容中與新內(nèi)容重疊的部分會被保留,并會在原有內(nèi)容之下繪制新圖形
            // destination-in : 原有內(nèi)容中與新圖形重疊的部分會被保留,其它區(qū)域都變成透明的
            // destination-out : 原有內(nèi)容中與新圖形不重疊的部分會被保留。

            // lighter : 兩圖形中重疊部分作加色處理。
            // copy: 只有新圖形會被保留,其它都被清除掉
            // xor : 重疊的部分會變成透明
            //darker 兩圖形中重疊的部分作減色處理
        </pre>
    </div>

CSS

<style>
        span {
            margin: 0 20px;
            border: 1px solid #CCC;
        }

        .active {
            background-color: orange;
            color: #FFF;
        }
    </style>

JS

var canvas = document.createElement('canvas');

canvas.width = 500;
canvas.height = 500;

document.body.appendChild(canvas);

var ctx = canvas.getContext('2d');

更改圖形重疊函數(shù)

function changeOperation(oper) {
    ctx.save();
    ctx.clearRect(0, 0, 500, 500);

    ctx.beginPath();
    ctx.fillStyle = 'green';
    ctx.fillRect(180, 150, 200, 200);

    ctx.globalCompositeOperation = oper || 'source-over';
    
    ctx.beginPath();

    ctx.fillStyle = 'orange';
    ctx.arc(300, 300, 100, 0, 2 * Math.PI);
    ctx.fill();
    ctx.restore();
}

changeOperation();

綁定點(diǎn)擊切換圖形事件:

var types = document.getElementById('types');
    var typeSpan = types.children;

    for (var i = 0; i < typeSpan.length; i++) {
        typeSpan.index = i;
    }

    types.onclick = function (e) {
        var tar = e.target;
        for (var i = 0; i < typeSpan.length; i++) {
            typeSpan[i].classList.remove('active');
        }
        tar.classList.add('active');
        changeOperation(tar.innerHTML);
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • canvas元素的基礎(chǔ)知識 在頁面上放置一個(gè)canvas元素,就相當(dāng)于在頁面上放置了一塊畫布,可以在其中進(jìn)行圖形的...
    oWSQo閱讀 10,445評論 0 19
  • 前文 我們在用canvas繪圖中,經(jīng)常會碰到當(dāng)倆個(gè)圖形重疊時(shí)怎么辦的問題,這篇文章介紹的就是一些關(guān)于倆個(gè)圖形重疊時(shí)...
    LinDaiDai_霖呆呆閱讀 6,337評論 0 3
  • ??HTML5 添加的最受歡迎的功能就是 元素。這個(gè)元素負(fù)責(zé)在頁面中設(shè)定一個(gè)區(qū)域,然后就可以通過 JavaScri...
    霜天曉閱讀 3,182評論 0 2
  • 弧度與sin及cos的關(guān)系 目的: 通過理解弧度與sin及cos的關(guān)系,可以根據(jù)弧度及半徑求出旋轉(zhuǎn)指定弧度后所到達(dá)...
    IOLG閱讀 2,191評論 0 0
  • 元素負(fù)責(zé)在頁面中設(shè)定一個(gè)區(qū)域,然后就可以通過 JavaScript 動態(tài)地在這個(gè)區(qū)域中繪制圖形。 一、基本用法 要...
    LemonnYan閱讀 18,804評論 0 7

友情鏈接更多精彩內(nèi)容