on master help sign in
Allow `fittedImage` to handle animated gifs!
commit64a1d9fa5b88638985467421ba41b6e76191ae6e authorevancz <info@elm-lang.org> dateSun 4 Nov 2012, 15:15 (14 years ago) parent4877290
1 file changed+20−20
core-js/Graphics/Render.js+20−20
core-js/Graphics/Render.js+20−20
@@ -28,32 +28,32 @@ function image(src) {
2828}
2929
3030function fittedImage(w,h,src) {
31− var canvas = newElement('canvas');
32− canvas.style.display = "block";
33− canvas.style.width = w + 'px';
34− canvas.style.height = h + 'px';
35− canvas.width = w;
36− canvas.height = h;
37− canvas.innerHTML = "Your browser does not support the canvas element.";
31+ var e = newElement('div');
32+ e.style.width = w + 'px';
33+ e.style.height = h + 'px';
34+ e.style.position = "relative";
35+ e.style.overflow = "hidden";
3836
3937 var img = newElement('img');
4038 img.onload = function() {
41− if (canvas.getContext) {
42− var ctx = canvas.getContext('2d');
43− var sx = 0, sy = 0, sWidth = this.width, sHeight = this.height;
44− if (w / h > this.width / this.height) {
45− sHeight = this.width * h / w;
46− sy = (this.height - sHeight) / 2;
47− } else {
48− sWidth = this.height * w / h;
49− sx = (this.width - sWidth) / 2;
50− }
51− ctx.drawImage(img, sx, sy, sWidth, sHeight,
52− 0,0, canvas.width, canvas.height);
39+ img.style.position = 'absolute';
40+ img.style.margin = 'auto';
41+
42+ var sw = w, sh = h;
43+ if (w / h > this.width / this.height) {
44+ sh = Math.round(this.height * w / this.width);
45+ } else {
46+ sw = Math.round(this.width * h / this.height);
5347 }
48+ img.style.width = sw + 'px';
49+ img.style.height = sh + 'px';
50+ img.style.left = ((w - sw) / 2) + 'px';
51+ img.style.top = ((h - sh) / 2) + 'px';
5452 };
5553 img.src = src;
56− return canvas;
54+ img.name = src;
55+ addTo(e,img);
56+ return e;
5757};
5858
5959var video = function(src) {