【问题标题】:Html5 canvas pattern drawing delayHtml5画布图案绘制延迟
【发布时间】:2017-05-05 04:06:19
【问题描述】:

我用图像()绘制一个画布(又名画布 1),然后将其旋转 25 度。然后我用旋转的画布为另一个画布(又名画布 2)制作图案。然后我画这个。并用新创建的图案填充填充样式。我注意到下面代码中间是否有警报

 finalsleeve_ctx.globalCompositeOperation = "source-atop";
           /*****************************************
              alert(sleeve.toDataURL('image/png'));
            *****************************************/
              var pattern = finalsleeve_ctx.createPattern(sleeve, 'repeat');

然后 firefox 会给出正确的输出,但如果我不发出警报,它不会给我正确的输出。 crom 没有显示正确的输出。

我需要延迟吗?

这是我尝试过的。

HTML

 <div >     
                <canvas id="sleeve" width=436 height=567></canvas>
                <canvas id="finalsleeve" width=436 height=567 ></canvas>

            </div>

JS

var sleeve = document.getElementById('sleeve');
var sleeve_ctx = sleeve.getContext('2d');

var finalsleeve = document.getElementById('finalsleeve');
var finalsleeve_ctx = finalsleeve.getContext('2d');
 function rotator2(var2,var3) 
{
   sleeve.width=sleeve.width;
   var imageObj_rotator2 = new Image();
   imageObj_rotator2.onload = function ()
   {
    var pattern_rotator2 = sleeve_ctx.createPattern(imageObj_rotator2, "repeat");
    sleeve_ctx.fillStyle = pattern_rotator2;
    sleeve_ctx.rect(0, 0, sleeve.width, sleeve.height);
    sleeve_ctx.rotate(var3 * Math.PI/180);
    sleeve_ctx.fill();
    }
    imageObj_rotator2.src = var2;

}
    function drawSleeve()
    {
      finalsleeve.width = finalsleeve.width;
      var imgsleeve = new Image();
      imgsleeve.src="http://i.stack.imgur.com/FoqGC.png";
      finalsleeve_ctx.drawImage(imgsleeve,0,0);
      finalsleeve_ctx.globalCompositeOperation = "source-atop";
      alert(sleeve.toDataURL('image/png'));
      var pattern = finalsleeve_ctx.createPattern(sleeve, 'repeat');
      finalsleeve_ctx.rect(0, 0, sleeve.width, sleeve.height);
      finalsleeve_ctx.fillStyle = pattern;
      finalsleeve_ctx.fill();
      finalsleeve_ctx.globalAlpha = .10;
      finalsleeve_ctx.drawImage(imgsleeve, 0, 0);
      finalsleeve_ctx.drawImage(imgsleeve, 0, 0);
      finalsleeve_ctx.drawImage(imgsleeve, 0, 0);
    }
rotator2('http://i.stack.imgur.com/fvpMN.png','25');
drawSleeve();

这是小提琴。 http://jsfiddle.net/EbBHz/

【问题讨论】:

    标签: javascript jquery html html5-canvas


    【解决方案1】:

    已编辑

    对不起,我完全误解了你的问题。我刚刚回过头来,看到了您发布的最后一个问题以及您要实现的目标。

    要获得您想要的功能,您只需创建一个功能,您不需要两个。我没有在 HTML 中使用第二个画布,而是使用 javascript 创建了一个临时画布。

    这是简化的功能代码

    <canvas id="sleeve" width='436' height='567'></canvas>
    
    var sleeve = document.getElementById('sleeve');
    var ctx = sleeve.getContext('2d');
    
    function rotator2(var2, var3) {
        // Draw the original sleeves
        var imageObj_rotator2 = new Image();
        imageObj_rotator2.src = var2;
        imageObj_rotator2.onload = function () {
            var imgsleeve = new Image();
            imgsleeve.src="http://i.stack.imgur.com/FoqGC.png";
            ctx.drawImage(imgsleeve,0,0);
    
        // Create a second temporary canvas
            var pattern = document.createElement('canvas');
            pattern.width = 500;
            pattern.height = 500;
            var pctx = pattern.getContext('2d');
       // Make the pattern that fills the generated canvas
            var pattern_rotator2 = pctx.createPattern(imageObj_rotator2, "repeat");
            pctx.fillStyle = pattern_rotator2; 
            pctx.rotate(var3 * Math.PI / 180);
       // Fill the generated canvas with the rotated image pattern we just created
            pctx.fillRect(0, 0, pattern.width, pattern.height);
    
       // Create a pattern of the generated canvas
            var patterned = ctx.createPattern(pattern, "repeat");
       // Fills in the non-transparent part of the image with whatever the 
       // pattern from the second canvas is
            ctx.globalCompositeOperation = "source-in";
            ctx.fillStyle = patterned;
            ctx.fillRect(0, 0, sleeve.width, sleeve.height);
        }
    }
    
    rotator2('http://i.stack.imgur.com/fvpMN.png', '45')
    

    该技术可以正常工作,但仅适用于某些角度。 Here is the demo 设置为 45 度。如您所见,有一个问题:袖子的一部分变白了。但是,如果您将度数更改为 15 like this,它就可以正常工作。这是因为当图像在创建的画布中旋转时,它会在重复之前留下空白。要直接查看此问题,请将创建的画布的宽度和高度更改为 30(图像的默认宽度/高度)like this

    注意:打开 jsfiddle 选项卡后,您可能必须单击运行,画布不喜欢在聚焦另一个选项卡时生成内容

    我尝试解决问题,包括

    • 使生成的画布变得非常大(这可行但会杀死负载 有时/崩溃页面)
    • 旋转生成的画布中的图片 没有像我希望的那样工作
    • 想出一个功能来改变宽度/高度以覆盖 基于旋转的第二画布维度的整个第一画布,这是迄今为止最有希望的,但我没有时间或不想制定一个好的解决方案

    如果角度必须是动态的,那么您可以为它设计一个函数。否则只需使用解决方法角度/生成的画布尺寸

    【讨论】:

      【解决方案2】:

      final result>这是一个在任何角度都没有白色填充旋转图案的工作解决方案

              var sleeve = document.getElementById('sleeve');
          var ctx = sleeve.getContext('2d');
      
          function rotator2(var2, var3) {
            var x =0;
            var y=0;
      
           //pattern size should be grater than height and width of object so that white space does't appear.
            var patternSize =  sleeve.width+ sleeve.height;
      
            // Draw the original sleeves
            var imageObj_rotator2 = new Image();
            imageObj_rotator2.src = var2;
            imageObj_rotator2.onload = function () {
            var imgsleeve = new Image();
            imgsleeve.src="http://i.stack.imgur.com/FoqGC.png";
            ctx.drawImage(imgsleeve,0,0);
      
            // Create a second temporary canvas
            var pattern = document.createElement('canvas');
            pattern.width = sleeve.width;
            pattern.height = sleeve.height;
            var pctx = pattern.getContext('2d');
            // Make the pattern that fills the generated canvas
            var pattern_rotator2 = pctx.createPattern(imageObj_rotator2, "repeat");
            pctx.fillStyle = pattern_rotator2; 
      
            //moving rotation point to center of target object.
            pctx.translate(x+ sleeve.width/2, y+sleeve.height/2);
            pctx.rotate(var3 * Math.PI / 180);
      
           // Fill the generated canvas with the rotated image pattern we just created and expanding size from center of rotated angle
            pctx.fillRect(-patternSize/2, -patternSize/2, patternSize, patternSize);
      
            // Create a pattern of the generated canvas
            var patterned = ctx.createPattern(pattern, "no-repeat");
            // Fills in the non-transparent part of the image with whatever the 
            // pattern from the second canvas is
            ctx.globalCompositeOperation = "source-in";
            ctx.fillStyle = patterned;
            ctx.fillRect(x, y, sleeve.width, sleeve.height);
          }
          }
      
          rotator2('http://i.stack.imgur.com/fvpMN.png', '50')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-22
        相关资源
        最近更新 更多