缩放图片数据
<pre><code class="language-javascript">/**
* 缩放图片数据
* @param {*} imageData
* @param {*} xGrid
* @param {*} yGrid
* @param {*} scale
*/
ScaleImageData(imageData, xGrid, yGrid, scaleFactor) {
const width = xGrid;
const height = yGrid;
const scaledWidth = width * scaleFactor;
const scaledHeight = height * scaleFactor;
// 创建一个新的ImageData对象,尺寸是原来的10倍
const scaledImageData = new Array(scaledWidth * scaledHeight);
// 遍历原始ImageData中的每个像素
for (let y = 0; y &lt; height; y++) {
for (let x = 0; x &lt; width; x++) {
const index = (y * width + x);
// 对于放大后的ImageData,我们复制原像素10次
for (let sy = 0; sy &lt; scaleFactor; sy++) {
for (let sx = 0; sx &lt; scaleFactor; sx++) {
const scaledIndex = ((y * scaleFactor + sy) * scaledWidth + x * scaleFactor + sx);
// scaledImageData.data.set(imageData.data.slice(index, index), scaledIndex);
scaledImageData[scaledIndex] = imageData[index];
}
}
}
}
return scaledImageData;
}</code></pre>