简体   繁体   English

HTML5画布图案绘制延迟

[英]Html5 canvas pattern drawing delay

i draw a canvas(aka canvas 1) with image( 我用图像绘制画布(aka canvas 1) 在此处输入图片说明 ) then rotate it 25 degree. ),然后将其旋转25度。 then i take rotated canvas to make a pattern for another canvas(aka canvas 2). 然后我以旋转的画布为另一个画布(又称为画布2)制作图案。 then i draw this 然后我画这个 在此处输入图片说明 . and fill the fillstyle with newly created pattern. 并用新创建的图案填充fillstyle。 i noticed if alert in the middle of below code 我注意到以下代码中间是否有警报

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

then firefox gives a correct output but if i dont do alert it does not give me correct output. 然后Firefox会给出正确的输出,但如果我不提醒它不会给出正确的输出。 crome not showing me correct output. crome没有显示正确的输出。

do i need to delay ? 我需要延迟吗?

here is what i have tried. 这是我尝试过的。

HTML 的HTML

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

            </div>

JS 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();

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

EDITED 已编辑

Sorry, I completely misunderstood your question. 抱歉,我完全误解了您的问题。 I just now went back and saw the last question you posted and the goal you are trying to achieve. 我刚才回过头去,看到了您发布的最后一个问题以及您要实现的目标。

To get the functionality you desire you can just create one function, you don't need two. 要获得所需的功能,您只需创建一个功能,而无需两个。 Instead of using a second canvas in the HTML I created a temporary one using javascript. 我没有在HTML中使用第二个画布,而是使用javascript创建了一个临时画布。

Here is the simplified and functional code 这是简化的功能代码

<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')

The technique works alright, but only for certain angles. 该技术可以正常工作,但仅适用于某些角度。 Here is the demo set to 45 degrees. 这是设置为45度的演示 As you can see, there is a problem: part of the sleeve is whited out. 如您所见,这里有一个问题:一部分袖子变白了。 However, if you change the degree to 15 like this it works just fine. 但是,如果像这样将度数更改为15,则效果很好。 This is because when the image is being rotated in the created canvas it leaves white space before repeating. 这是因为在创建的画布中旋转图像时,在重复之前会留有空白。 To see this issue first hand, change the width and the height of the created canvas to 30 (the default width/height of the image) like this 看到这个问题第一手,改变宽度和所创建画布的〜30的高度(图像的默认宽度/高度) 这样

Note : You may have to click run once the jsfiddle tab is open, canvases don't like generating content when another tab is focused 注意 :jsfiddle选项卡打开后,您可能必须单击运行,当另一个选项卡聚焦时,画布不喜欢生成内容

I tried problem solving the issue including 我尝试解决问题,包括

  • Making the generated canvas really large (which works but KILLS load time/crashes page sometimes) 使生成的画布真的很大(可以工作,但有时会加载时间/崩溃页面)
  • Translating the picture in the generated canvas after rotating it which didn't work like I had hoped 旋转后在生成的画布中翻译图片,但效果不尽如人意
  • Coming up with a function to change the width/height to cover the entire first canvas based on the rotated second-canvas-dimensions, which is by far the most promising, but I don't have the time or desire to work out a good solution 提出了一个功能,该功能可以根据旋转的第二画布尺寸来更改宽度/高度,以覆盖整个第一画布,这是迄今为止最有希望的方法,但是我没有时间或渴望制定出一个好的解

All that being said if the angle HAS to be dynamic you can work on a function for it. 可以说,如果角度必须是动态的,则可以对其进行处理。 Otherwise just use a workaround angle/generated canvas dimensions 否则,只需使用解决方法角度/生成的画布尺寸

final result > Here is a working solution for fill rotated pattern without white at any angle 最终结果 >这是用于填充旋转图案的工作解决方案,该图案在任何角度都没有白色

        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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM