【问题标题】:How to use HTML5 <canvas> clip()如何使用 HTML5 <canvas> clip()
【发布时间】:2011-08-13 15:41:39
【问题描述】:

我是 HTML5 &lt;canvas&gt; 的新手,正在尝试制作一些东西,实际上是绘制 PORTAL2 徽标:)。

到现在为止

但是你可以看到腿从墙上伸出来,我想知道如何去除多余的油漆。

我想这可以通过clip() 函数完成,但我不知道如何使用它。

这就是我想要的

这是我正在尝试的代码,也可在此处的 JS Bin http://jsbin.com/exado5/edit 获得

//function to convert deg to radian
function acDegToRad(deg)
{
     return deg* (-(Math.PI / 180.0));    
}

//set fill color to gray
ctx.fillStyle = "rgb(120,120,120)";
//save the current state with fillcolor
ctx.save();

//draw 2's base rectangle
ctx.fillRect(20,200,120,35);
//bring origin to 2's base
ctx.translate(20,200);
//rotate the canvas 35 deg anti-clockwise
ctx.rotate(acDegToRad(35));
//draw 2's slant rectangle
ctx.fillRect(0,0,100,35);
//restore the canvas to reset transforms
ctx.restore();
//set stroke color width and draw the 2's top semi circle
ctx.strokeStyle = "rgb(120,120,120)";
ctx.lineWidth = 35;
ctx.beginPath();
ctx.arc(77,135,40,acDegToRad(-40),acDegToRad(180),true);
ctx.stroke();

//reset canvas transforms
ctx.restore();

//change color to blue
ctx.fillStyle = "rgb(0,160,212)";
//save current state of canvas
ctx.save();
//draw long dividing rectangle 
ctx.fillRect(162,20,8,300);
//draw player head circle
ctx.beginPath();
ctx.arc(225,80,35,acDegToRad(0),acDegToRad(360));
ctx.fill();

//start new path for tummy :)
ctx.beginPath();
ctx.moveTo(170,90);
ctx.lineTo(230,140);
ctx.lineTo(170,210);
ctx.fill();

//start new path for hand
//set lineCap and lineJoin to "round", blue color 
//for stroke, and width of 25px
ctx.lineWidth = 25;
ctx.lineCap = "round";
ctx.strokeStyle = "rgb(0,160,212)";
ctx.lineJoin = "round";
ctx.beginPath();
ctx.moveTo(222,150);
ctx.lineTo(230,190);
ctx.lineTo(270,220);
ctx.stroke();

//begin new path for drawing leg
ctx.beginPath();
ctx.moveTo(160,210);
ctx.lineTo(195,260);
ctx.lineTo(160,290);
ctx.stroke();

请帮忙。

【问题讨论】:

  • i) 将其设为空白的白色垂直线 ii) 重叠并靠近蓝色垂直线...
  • ummmm.. 不知道你的意思.. 但是当我打电话给剪辑时,它也会夹住膝盖:(

标签: javascript html canvas clip


【解决方案1】:

添加这个:

...
//reset canvas transforms
ctx.restore();

ctx.beginPath();
ctx.moveTo(162, 20);
ctx.lineTo(162, 320);
ctx.lineTo(300, 320);
ctx.lineTo(300, 20);
ctx.clip();

//change color to blue
ctx.fillStyle = "rgb(0,160,212)";
//save current state of canvas
...

【讨论】:

    【解决方案2】:

    更改代码并检查它

    //change color to blue
    ctx.fillStyle = "rgb(0,160,212)";
    //save current state of canvas
    ctx.save();
    //draw long dividing rectangle
    ctx.fillRect(157,20,15,300);
    //draw player head circle
    ctx.beginPath();
    ctx.arc(225,80,35,acDegToRad(0),acDegToRad(360));
    ctx.fill();
    
    //start new path for tummy :)
    ctx.beginPath();
    ctx.moveTo(170,90);
    ctx.lineTo(230,140);
    ctx.lineTo(170,210);
    ctx.fill();
    
    //start new path for hand
    //set lineCap and lineJoin to "round", blue color
    //for stroke, and width of 25px
    ctx.lineWidth = 25;
    ctx.lineCap = "round";
    ctx.strokeStyle = "rgb(0,160,212)";
    ctx.lineJoin = "round";
    ctx.beginPath();
    ctx.moveTo(222,150);
    ctx.lineTo(230,190);
    ctx.lineTo(270,220);
    ctx.stroke();
    
    //begin new path for drawing leg
    ctx.beginPath();
    ctx.moveTo(170,210);
    ctx.lineTo(200,260);
    ctx.lineTo(170,290);
    ctx.stroke();
    

    【讨论】:

    • 这工作..但你只是增加墙的宽度......不幸的是那不是我想要的
    • 对不起,我正在努力让它尽可能接近原作
    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多