开发者

HTML5 Canvas... Any way can i short my code if i have 2+ images?

http://jsfiddle.net/TCdpV/1/

<canvas id="img1" width="230" height="150" class="mainimage_large" style="margin: 0 0 0 10px;">
</canvas>


<canvas id="img2" width="230" height="150" class="mainimage_large" style="margin: 0 0 0 10px;">
</canvas>

this is my JS

var canvas1 = document.getElementById('img1');
var ctx1 = canvas1.getContext('2d');
var img1 = document.createElement('IMG');
img1.onload = function () {

    ctx1.save();
    ctx1.beginPath();
    ctx1.moveTo(23, 0);
    ctx1.lineTo(250, 0);
    ctx1.lineTo(250, 150);
    ctx1.lineTo(0, 150);
    ctx1.lineTo(0, 30);
    ctx1.closePath();
    ctx1.clip();
    ctx1.drawImage(img1, 0, 0);
    ctx1.restore();
}
img1.src = "http://s1.jrnl.ie/media/2011/05/Ming-Ming-41-230x150.jpg";

开发者_如何学Python




var canvas2 = document.getElementById('img2');
var ctx2 = canvas2.getContext('2d');
var img2 = document.createElement('IMG');
img2.onload = function () {

    ctx2.save();
    ctx2.beginPath();
    ctx2.moveTo(23, 0);
    ctx2.lineTo(250, 0);
    ctx2.lineTo(250, 150);
    ctx2.lineTo(0, 150);
    ctx2.lineTo(0, 30);
    ctx2.closePath();
    ctx2.clip();
    ctx2.drawImage(img2, 0, 0);
    ctx2.restore();
}
img2.src = "http://www.reisenews-online.de/wp-content/uploads/2011/03/Pandab%C3%A4r-Fu-HU-230x150.jpg";

Any best way to short my code ?

BTW.. i use in my Wordpress loop ...


You just want to make the code shorter?

http://jsfiddle.net/TCdpV/3/


heh i really tried to answer but system is being idiotic without css, telling

Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.

but it was, i gave 7 tries now i am pissed off ;)



just make a function with 'id' and 'url' params if it is what you are looking for, the rest seems short enough to me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜