【问题标题】:html5 - drawing multiple circles - each filled with different imageshtml5 - 绘制多个圆圈 - 每个圆圈都填充了不同的图像
【发布时间】:2013-03-15 11:22:51
【问题描述】:

好吧,所以我环顾四周,找到了一个代码,它可以让我成功地在画布上绘制一个圆圈并将该圆圈用作我的图像的蒙版。

代码如下所示:(对我不认识的真正创建者的 codus)

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

ctx.arc(100,100, 50, 0, Math.PI*2,true); // you can use any shape
ctx.clip();

var img = new Image();
img.addEventListener('load', function(e) {
    ctx.drawImage(this, 0, 0, 200, 300);
}, true);
img.src="/path/to/image.jpg";

假设我想要 5 个不同的圆圈,所有圆圈都有不同的图像,并且每个圆圈的位置都不同。

有人知道 id 是怎么做的吗?

【问题讨论】:

  • 简而言之,如果上面的代码对 1 张图片有效,则对 5 张图片使用 5 次。或者为每个图像更改的值创建一个带有参数的函数。我认为上面代码中缺少的主要是save()和restore()的使用,以允许代码多次使用。

标签: javascript html canvas html5-canvas


【解决方案1】:

为保持代码简短,请创建一个函数,其中包含将在图像之间更改的设置的参数。

可复用功能:

function drawImageCircle(ctx, circleX, circleY, radius,
                              imageX, imageY, imageWidth, imageHeight, imageUrl) {

    var img = new Image();
    img.onload = function(){
        ctx.save();
        ctx.beginPath();
        ctx.arc(circleX, circleY, radius, 0, Math.PI*2, true);
        ctx.clip();
        ctx.drawImage(this, imageX, imageY, imageWidth, imageHeight);
        ctx.restore();
    };
    img.src = imageUrl;
}

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

drawImageCircle(ctx, 100,100, 50,  0,0,     200,300, 'image1.jpg');
drawImageCircle(ctx, 400,400, 50,  300,300, 200,300, 'image2.jpg');

多次使用save() 和restore() 很重要。

【讨论】:

  • 我喜欢你关于可重用函数的想法,但要让它工作,你可能必须进行图像预加载并将实际图像提供给它,而不仅仅是 imageUrl。顺便说一句,img.src=imageUrl 应该在 img.onload 之后;)
  • @markE:诚然,如果没有预加载,图像将被异步绘制(以某种随机顺序),如果它们重叠可能是不可接受的(导致错误的图像有时在顶部)。关于img.src 和img.onload 的顺序很好!感谢您指出了这一点!我更新了。
【解决方案2】:

是的,和马特所说的差不多……

这是代码和小提琴:http://jsfiddle.net/m1erickson/Vu2Fm/

您可以通过使用图像预加载器在画布上绘制之前加载所有 5 个图像来改进此代码。

<!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 img1=new Image();
    img1.onload=function(){

        var img2=new Image();
        img2.onload=function(){

            // draw a clipping circle and then an image to clip
            ctx.save();
            ctx.beginPath();
            ctx.strokeStyle="blue";
            ctx.arc(100, 100, 50, 0 , 2 * Math.PI, false);
            ctx.stroke();
            ctx.clip();
            ctx.beginPath();
            ctx.arc(100, 100, 50, 0 , 2 * Math.PI, false);
            ctx.drawImage(img1,10,0);
            ctx.restore();

            // draw a second clipping circle and then an image to clip
            ctx.save();
            ctx.beginPath();
            ctx.strokeStyle="green";
            ctx.arc(275, 100, 75, 0 , 2 * Math.PI, false);
            ctx.stroke();
            ctx.clip();
            ctx.beginPath();
            ctx.drawImage(img2,150,0);
            ctx.restore();

        }
        img2.src="http://dl.dropbox.com/u/139992952/coffee.png";
    }
    img1.src="http://dl.dropbox.com/u/139992952/house%20vector.png";

}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=400 height=250></canvas>
</body>
</html>

【讨论】:

  • 嘿,马克 - 感谢您的评论 - 对我来说意义重大,并且肯定可以作为下面更时尚的解决方案的替代品为我工作
猜你喜欢
  • 2011-08-03
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多