我試圖在畫布中使用cover
模擬顯示圖像。我發現了一些很酷的answer關於如何做到這一點。如何在畫布中縮放圖像?
到目前爲止,我的圖像根據屏幕分辨率而變化,但只有在刷新頁面後纔會發生變化。
我怎樣可以得到以下scaling effect無刷新頁面? 嘗試調整窗口的大小。
HTML
<canvas id="canvas"></canvas>
JS
var ctx = canvas.getContext('2d'),
img = new Image;
canvas.setAttribute('width', window.innerWidth);
canvas.setAttribute('height', window.innerHeight);
img.onload = draw;
img.src = 'https://upload.wikimedia.org/wikipedia/commons/0/0f/2010-02-19_3000x2000_chicago_skyline.jpg';
function draw() {
drawImageProp(ctx, this, 0, 0, canvas.width, canvas.height);
}
/**
* By Ken Fyrstenberg
*
* drawImageProp(context, image [, x, y, width, height [,offsetX, offsetY]])
*
* If image and context are only arguments rectangle will equal canvas
*/
function drawImageProp(ctx, img, x, y, w, h, offsetX, offsetY) {
if (arguments.length === 2) {
x = y = 0;
w = ctx.canvas.width;
h = ctx.canvas.height;
}
/// default offset is center
offsetX = offsetX ? offsetX : 0.5;
offsetY = offsetY ? offsetY : 0.5;
/// keep bounds [0.0, 1.0]
if (offsetX < 0) offsetX = 0;
if (offsetY < 0) offsetY = 0;
if (offsetX > 1) offsetX = 1;
if (offsetY > 1) offsetY = 1;
var iw = img.width,
ih = img.height,
r = Math.min(w/iw, h/ih),
nw = iw * r, /// new prop. width
nh = ih * r, /// new prop. height
cx, cy, cw, ch, ar = 1;
/// decide which gap to fill
if (nw < w) ar = w/nw;
if (nh < h) ar = h/nh;
nw *= ar;
nh *= ar;
/// calc source rectangle
cw = iw/(nw/w);
ch = ih/(nh/h);
cx = (iw - cw) * offsetX;
cy = (ih - ch) * offsetY;
/// make sure source rectangle is valid
if (cx < 0) cx = 0;
if (cy < 0) cy = 0;
if (cw > iw) cw = iw;
if (ch > ih) ch = ih;
/// fill image in dest. rectangle
ctx.drawImage(img, cx, cy, cw, ch, x, y, w, h);
}
需要注意的是縮放在畫布上不起作用。在畫布上繪製的內容不能像那樣操作。應該在調整大小的畫布上重新繪製圖像。 – rottenoats
@Grimbode:我想我現在已經知道了。 – Cerbrus
是否最好的做法是清理畫布,然後重繪或重繪得夠好,如果之前的繪圖不再可見?這對性能有什麼影響? –