<i id='oLWCt'><tr id='oLWCt'><dt id='oLWCt'><q id='oLWCt'><span id='oLWCt'><b id='oLWCt'><form id='oLWCt'><ins id='oLWCt'></ins><ul id='oLWCt'></ul><sub id='oLWCt'></sub></form><legend id='oLWCt'></legend><bdo id='oLWCt'><pre id='oLWCt'><center id='oLWCt'></center></pre></bdo></b><th id='oLWCt'></th></span></q></dt></tr></i><div id='oLWCt'><tfoot id='oLWCt'></tfoot><dl id='oLWCt'><fieldset id='oLWCt'></fieldset></dl></div>
      <legend id='oLWCt'><style id='oLWCt'><dir id='oLWCt'><q id='oLWCt'></q></dir></style></legend>
      <tfoot id='oLWCt'></tfoot>
    1. <small id='oLWCt'></small><noframes id='oLWCt'>

        <bdo id='oLWCt'></bdo><ul id='oLWCt'></ul>

      在画布上的图像不透明部分周围绘制边框

      Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)

      <legend id='LK007'><style id='LK007'><dir id='LK007'><q id='LK007'></q></dir></style></legend>
        <tfoot id='LK007'></tfoot>

            <bdo id='LK007'></bdo><ul id='LK007'></ul>
          • <i id='LK007'><tr id='LK007'><dt id='LK007'><q id='LK007'><span id='LK007'><b id='LK007'><form id='LK007'><ins id='LK007'></ins><ul id='LK007'></ul><sub id='LK007'></sub></form><legend id='LK007'></legend><bdo id='LK007'><pre id='LK007'><center id='LK007'></center></pre></bdo></b><th id='LK007'></th></span></q></dt></tr></i><div id='LK007'><tfoot id='LK007'></tfoot><dl id='LK007'><fieldset id='LK007'></fieldset></dl></div>

                <small id='LK007'></small><noframes id='LK007'>

                  <tbody id='LK007'></tbody>
                本文介绍了在画布上的图像不透明部分周围绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 drawImage 在画布上绘制图像.这是一个被透明像素包围的PNG,如下所示:

                I'm drawing an image onto a canvas using drawImage. It's a PNG that is surrounded by transparent pixels, like this:

                如何为画布上该图像的可见部分添加纯色边框?澄清一下:我不想要一个围绕图像边界框的矩形.边界应该绕过草地.

                How can I add a solid-colored border to the visible part of that image on the canvas? To clarify: I don't want a rectangle that surrounds the image's bounding box. The border should go around the grass patch.

                我确实考虑过使用阴影,但我真的不想要一个发光的边框,我想要一个实心的.

                I did consider using shadows, but I don't really want a glowing border, I want a solid one.

                推荐答案

                有点晚了,不过画个图offset,比分析边缘快很多:

                A bit late, but just draw the image offset which is much faster than analyzing the edges:

                var ctx = canvas.getContext('2d'),
                    img = new Image;
                
                img.onload = draw;
                img.src = "http://i.stack.imgur.com/UFBxY.png";
                
                function draw() {
                
                  var dArr = [-1,-1, 0,-1, 1,-1, -1,0, 1,0, -1,1, 0,1, 1,1], // offset array
                      s = 2,  // thickness scale
                      i = 0,  // iterator
                      x = 5,  // final position
                      y = 5;
                  
                  // draw images at offsets from the array scaled by s
                  for(; i < dArr.length; i += 2)
                    ctx.drawImage(img, x + dArr[i]*s, y + dArr[i+1]*s);
                  
                  // fill with color
                  ctx.globalCompositeOperation = "source-in";
                  ctx.fillStyle = "red";
                  ctx.fillRect(0,0,canvas.width, canvas.height);
                  
                  // draw original image in normal mode
                  ctx.globalCompositeOperation = "source-over";
                  ctx.drawImage(img, x, y);
                }

                <canvas id=canvas width=500 height=500></canvas>

                这篇关于在画布上的图像不透明部分周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)
                How to show a running progress bar while page is loading(如何在页面加载时显示正在运行的进度条)
                  <tbody id='aWKDA'></tbody>
                <legend id='aWKDA'><style id='aWKDA'><dir id='aWKDA'><q id='aWKDA'></q></dir></style></legend>

                  <small id='aWKDA'></small><noframes id='aWKDA'>

                  • <bdo id='aWKDA'></bdo><ul id='aWKDA'></ul>

                      <tfoot id='aWKDA'></tfoot>

                        <i id='aWKDA'><tr id='aWKDA'><dt id='aWKDA'><q id='aWKDA'><span id='aWKDA'><b id='aWKDA'><form id='aWKDA'><ins id='aWKDA'></ins><ul id='aWKDA'></ul><sub id='aWKDA'></sub></form><legend id='aWKDA'></legend><bdo id='aWKDA'><pre id='aWKDA'><center id='aWKDA'></center></pre></bdo></b><th id='aWKDA'></th></span></q></dt></tr></i><div id='aWKDA'><tfoot id='aWKDA'></tfoot><dl id='aWKDA'><fieldset id='aWKDA'></fieldset></dl></div>