请参阅所附图片。我希望 retangle(边框)不与其他图像重叠。出于某种奇怪的原因,只有添加到画布的最后一张图像正确显示。
这是我的代码
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="600" height="600"></canvas>
<script>
function loadImages(sources, callback) {
var images = {};
var loadedImages = 0;
var numImages = 0;
// get num of sources
for(var src in sources) {
numImages++;
}
for(var src in sources) {
images[src] = new Image();
images[src].onload = function() {
if(++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
}
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var sources = {
bg: 'bg2.jpg',
a: 'a.jpg',
b: 'b.jpg',
c: 'c.jpg',
d: 'd.jpg',
e: 'e.jpg',
f: 'f.jpg'
};
function drawImageRot(img,x,y,width,height,deg){
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var rad = deg * Math.PI / 180;
ctx.translate(x + width / 2, y + height / 2);
ctx.rotate(rad);
ctx.rect(width / 2 * (-1), height / 2 * (-1), width, height);
ctx.stroke();
ctx.strokeStyle = 'white';
ctx.lineWidth = 2;
ctx.drawImage(img,width / 2 * (-1),height / 2 * (-1),width,height);
ctx.rotate(rad * ( -1 ) );
ctx.translate((x + width / 2) * (-1), (y + height / 2) * (-1));
}
loadImages(sources, function(images) {
drawImageRot(images.bg, 0, 0, 600, 600,0);
drawImageRot(images.b, 380, 55,277, 184,-20);
drawImageRot(images.a,-20,-20, 300, 224,-30);
drawImageRot(images.e, 130, 150,350, 274,0);
drawImageRot(images.d, 320, 430,277, 184,20);
drawImageRot(images.f, 0, 380, 240,160,-10);
});
</script>