【发布时间】:2017-06-02 00:40:24
【问题描述】:
所以,我有一个浏览器游戏,它由具有动态尺寸(它缩放到用户窗口的尺寸)的画布运行,它获取各种尺寸的图像,根据用户的窗口尺寸缩放它们并绘制它们: (我的调整大小算法示例:)
https://jsfiddle.net/8o3sn9mh/55/
var
canvas = document.getElementById('c'),
ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var flower = new Image();
//the src is actually local, but i put a url for example's sake
flower.src = "http://plainicon.com/dboard/userprod/2803_dd580/prod_thumb/plainicon.com-46129-128px-af2.png"
flower.onload = function(){
ctx.drawImage(flower,0,0,128,128, 0, 0, window.innerWidth / 2, window.innerHeight / 2);
}
如您所见,我正在绘制一个 128x128px 的图像,并将其调整为用户窗口的一半。所以,我在想 - 我的游戏可能会在每次渲染执行时执行此操作,并且每次执行它可能需要不必要的调整大小时间。所以:
1) 由于我每个 requestAnimationFrame 都使用它,因此执行此调整大小是否需要更多时间?
2) 有没有办法一次调整我的图像大小,将其保存到 var 中并使用它来节省时间?
无论如何,我想要一种方法将调整大小的图像保存到 var 中,以便将其放入画布的 createPattern 方法中:
pattern = ctx.createPattern(resizedFlower, 'repeat'); //resizedFlower should be the original flower resized into half's the user's window's width + height
ctx.fillStyle = pattern;
ctx.fillRect(0, 0, 50, 50);
【问题讨论】:
标签: javascript canvas resize