2014-07-05 100 views
0

我想繪製多剪裁區域的區域。如何在html5中組合多個裁剪區域2d canvas畫圖

這裏是屏幕截圖:

enter image description here

我寫的描繪示例:http://jsfiddle.net/younyzhU/zR9hg/1/

總面積應該是四個​​面積+白色中心矩形。

從基本教程中,我知道繪製剪貼平面就像繪製別人的東西。

  1. 創建剪貼區域
  2. context.clip()

然而,創建剪貼區域,我們具有多區域,如何組合,任何建議? 謝謝。

下面是一些代碼:

ctx.save(); // save the context so we don't mess up others 
    ctx.beginPath(); 
    ctx.fillStyle = "#ffffff"; 
    ctx.rect(this.x, this.y, this.w, this.h); 
    ctx.fill(); 
    ctx.restore(); // restore context to what it was on entry 
    ctx.fillStyle = "#00ff00";//Color for four surrounded area. 
    ctx.save(); // save the context so we don't mess up others 
    ctx.beginPath(); 
    r = Math.sqrt((this.h/2)*(this.h/2) + 16 *this.w * this.w); 
    thea = Math.atan(this.h/this.w/8); 
    ctx.arc(this.x + this.w*4, this.y + this.h/2, r , Math.PI - thea, Math.PI+thea, false); 
    ctx.closePath(); 
    ctx.fill(); 
    //ctx.clip(); 
    ctx.restore(); // restore context to what it was on entry 

    ctx.save(); // save the context so we don't mess up others 
    ctx.beginPath(); 
    r = Math.sqrt((this.h/2)*(this.h/2) + 16 *this.w * this.w); 
    thea = Math.atan(this.h/this.w/8); 
    ctx.arc(this.x- this.w*3, this.y + this.h/2, r , -thea, thea, false); 
    ctx.closePath(); 
    ctx.fill(); 
    //ctx.clip(); 
    ctx.restore(); // restore context to what it was on entry 

    ctx.save(); // save the context so we don't mess up others 
    ctx.beginPath(); 
    r = Math.sqrt((this.w/2)*(this.w/2) + 16 * this.h * this.h); 
    thea = Math.atan(this.w/this.h/8); 
    ctx.arc(this.x + this.w/2, this.y + 4 * this.h, r , Math.PI*3/2-thea, Math.PI*3/2 + thea, false); 
    ctx.closePath(); 
    ctx.fill(); 
    ctx.restore(); // restore context to what it was on entry 
    ctx.save(); // save the context so we don't mess up others 
    ctx.beginPath(); 

    r = Math.sqrt((this.w/2)*(this.w/2) + 16*this.h*this.h); 
    thea = Math.atan(this.w/this.h/8); 
    ctx.arc(this.x + this.w/2, this.y-3*this.h , r , Math.PI/2-thea, Math.PI/2 + thea, false); 
    ctx.closePath(); 
    ctx.fill(); 
    ctx.restore(); // restore context to what it was on entry 
    ctx.save(); 

回答

2

剪輯路徑其實是一個路徑。

這意味着如果您在一條路徑中繪製了所有4個閉合弧線,則可以使用該多形狀路徑進行裁剪。

您可以通過在弧線之前執行1 beginPath命令而不是在每個弧線之前執行beginPath來組合您的路徑。

結果是看起來像這樣,你可以爲剪貼路徑使用單一的路徑:

enter image description here

例如,這是剪切路徑如何可以用來包含的對角的圖像條紋:

enter image description here

這裏的示例代碼和演示:http://jsfiddle.net/m1erickson/LYR3E/

<!doctype html> 
<html> 
<head> 
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css --> 
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
<style> 
    body{ background-color: ivory; } 
    canvas{border:1px solid red;} 
</style> 
<script> 
$(function(){ 

    var canvas=document.getElementById("canvas"); 
    var ctx=canvas.getContext("2d"); 

    var img=new Image(); 
    img.onload=start; 
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/rainbowDiagonal.jpg"; 
    function start(){ 

     ctx.beginPath(); 
     ctx.fillStyle = "#000"; 
     ctx.rect(0,0,canvas.width,canvas.height); 
     ctx.fill(); 

     draw(); 
    } 

    function draw(){ 
     var x=50; 
     var y=50; 
     var w=200; 
     var h=200; 

     ctx.save(); 

     ctx.fillStyle = "#00ff00";//Color for four surrounded area. 

     ctx.beginPath(); 

     r = Math.sqrt((h/2)*(h/2) + 16 *w * w); 
     thea = Math.atan(h/w/8); 
     ctx.arc(x + w*4, y + h/2, r , Math.PI - thea, Math.PI+thea, false); 
     ctx.closePath(); 

     r = Math.sqrt((h/2)*(h/2) + 16 *w * w); 
     thea = Math.atan(h/w/8); 
     ctx.arc(x- w*3, y + h/2, r , -thea, thea, false); 
     ctx.closePath(); 

     r = Math.sqrt((w/2)*(w/2) + 16 * h * h); 
     thea = Math.atan(w/h/8); 
     ctx.arc(x + w/2, y + 4 * h, r , Math.PI*3/2-thea, Math.PI*3/2 + thea, false); 
     ctx.closePath(); 

     r = Math.sqrt((w/2)*(w/2) + 16*h*h); 
     thea = Math.atan(w/h/8); 
     ctx.arc(x + w/2, y-3*h , r , Math.PI/2-thea, Math.PI/2 + thea, false); 
     ctx.closePath(); 

     ctx.clip(); 

     ctx.drawImage(img,0,0); 

     ctx.restore(); 

    } 

}); // end $(function(){}); 
</script> 
</head> 
<body> 
    <canvas id="canvas" width=300 height=300></canvas> 
</body> 
</html> 
+0

謝謝,創建一個剪輯區域只是結合一條路徑?好吧,我想我可以使用多路徑組合。再次感謝。 – yongnan

+0

是的,剪切路徑總是隻有一條路徑。但是,您可以在單一路徑中組合儘可能多的繪圖命令。 ;-) – markE