简体   繁体   English

画布:将照片绑定到框架中

[英]canvas: bind photo into the frame

Good day! 美好的一天!

I have a problem with merging canvas images. 合并画布图像时出现问题。 This questions will be mark as reply I think but I didn't find solution for my problem. 我认为这些问题将被标记为答复,但我没有找到解决问题的方法。

So this the deal: Get 3 canvas - 1 and 2 are images(must be hidden i think), 3 - merged canvas. 所以这笔交易:获得3个画布-1和2是图像(我认为必须隐藏),3-合并的画布。 1 is a frame , 2 is a photo , photo must be under frame (at css I could make it with z-index ). 1是框架 ,2是照片 ,照片必须框架 (在css中,我可以使用z-index )。

Html: HTML:

<div class="container">

    <canvas id="first" width="400" height="400"></canvas>

    <canvas id="second" width="200" height="200"></canvas>

    <canvas id="third" width="400" height="400"></canvas>

</div>

JS: JS:

var imgFirst = document.getElementById("first");
ctx1 = imgFirst.getContext('2d');
pic1 = new Image();
pic1.src = "http://www.deftune.com/wp-content/uploads/2010/10/thexx-400x400.jpg";


var imgSecond = document.getElementById("second");
ctx2 = imgSecond.getContext('2d');
pic2 = new Image();
pic2.src = "http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png";

var imgThird = document.getElementById('third');
ctx3 = imgThird.getContext('2d');
pic1.onload = function() {
    pic2.onload = function() {
        ctx3.drawImage(pic1, 0, 0, 400, 400);
        ctx3.drawImage(pic2, 100, 100, 200, 200);
    };
};

I've got jsfiddle example: http://jsfiddle.net/hh6ovneq/3/ but it not works 我有jsfiddle示例: http : //jsfiddle.net/hh6ovneq/3/但它不起作用

There are a couple of things that could simplify your code a lot. 有几件事可以大大简化您的代码。 First of, since you are only loading image resources, use the img tag instead. 首先,由于您仅加载图像资源,请改用img标签。 Then create a callback function and trigger it when both images have loaded. 然后创建一个回调函数,并在两个图像都加载后触发它。

Since I am unclear what your photo is and what your frame is I currently use the inversed order that you had, but just switching the two drawImage functions should switch their order (or z-index, hopwever you want to describe it). 由于我不清楚您的照片是什么,框架是什么,我目前使用的是相反的顺序,但是只需切换两个drawImage函数即可切换其顺序(或z-index,无论您要描述什么)。 Because your top image doesn't have transparent values, though, it will hide the bottom image as the pixels are overwritten with new colors. 但是,由于顶部图像没有透明值,因此当像素被新颜色覆盖时,它将隐藏底部图像。

The result would look something like this, which looks a lot cleaner and more readable: 结果看起来像这样,看起来更干净,更易读:

 // Get all your images. var first = document.getElementById('first'); var second = document.getElementById('second'); // get canvas and context var result = document.getElementById('result'); var context = result.getContext('2d'); // create a counter to make sure all images are loaded before drawing var loaded = 0; // something to execute when loading of images has completed function combine(){ // The order of drawing will be the order of layers // The final two arguments define the size of what you want to draw context.drawImage(second, 0, 0, 400, 400); context.drawImage(first, 100, 100, 200, 200); } // Add event listeners to the images first.addEventListener('load', function(){ // increase the loaded number loaded++; // hide the image first.style.display = 'none'; // if loaded hits the total number of images, use the completion function if(loaded === 2) combine(); }, false); // (wrince, repeat for image 2) second.addEventListener('load', function(){ loaded++; second.style.display = 'none'; if(loaded === 2) combine(); }, false); 
 <div class="container"> <img id="first" width="400" height="400" src="http://www.deftune.com/wp-content/uploads/2010/10/thexx-400x400.jpg" /> <img id="second" width="200" height="200" src="http://tech21info.com/admin/wp-content/uploads/2013/03/chrome-logo-200x200.png" /> <canvas id="result" width="400" height="400"></canvas> </div> 

Lastly I would like to add that your nested onload function could backfire on you when the second image is loaded before the first, which would mean that the load event does not get triggered anymore. 最后,我想补充一点,当在第二张图像之前加载第二张图像时,嵌套的onload函数可能会适得其反,这意味着load事件不再触发。 Use separate onloads and count them in order to be sure the outcome is correct. 使用单独的加载并计数它们,以确保结果正确。

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

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