日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

html5图像调整大小,JavaScript调整HTML5画布中图像的大小

發(fā)布時間:2023/12/19 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html5图像调整大小,JavaScript调整HTML5画布中图像的大小 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

因此,如果所有瀏覽器(實際上,Chrome 5給我提供了一個不錯的瀏覽器)都無法為你提供足夠好的重采樣質(zhì)量,你該怎么辦?然后,你自己實現(xiàn)它們!哦,來吧,我們正在進(jìn)入Web 3.0的新時代,符合HTML5的瀏覽器,超級優(yōu)化的JIT javascript編譯器,具有大量內(nèi)存的多核(?)機(jī)器,你擔(dān)心什么javascript中有java一詞,因此應(yīng)該保證性能,對嗎?看一下,縮略圖生成代碼:

// returns a function that calculates lanczos weight

function lanczosCreate(lobes) {

return function(x) {

if (x > lobes)

return 0;

x *= Math.PI;

if (Math.abs(x) < 1e-16)

return 1;

var xx = x / lobes;

return Math.sin(x) * Math.sin(xx) / x / xx;

};

}

// elem: canvas element, img: image element, sx: scaled width, lobes: kernel radius

function thumbnailer(elem, img, sx, lobes) {

this.canvas = elem;

elem.width = img.width;

elem.height = img.height;

elem.style.display = "none";

this.ctx = elem.getContext("2d");

this.ctx.drawImage(img, 0, 0);

this.img = img;

this.src = this.ctx.getImageData(0, 0, img.width, img.height);

this.dest = {

width : sx,

height : Math.round(img.height * sx / img.width),

};

this.dest.data = new Array(this.dest.width * this.dest.height * 3);

this.lanczos = lanczosCreate(lobes);

this.ratio = img.width / sx;

this.rcp_ratio = 2 / this.ratio;

this.range2 = Math.ceil(this.ratio * lobes / 2);

this.cacheLanc = {};

this.center = {};

this.icenter = {};

setTimeout(this.process1, 0, this, 0);

}

thumbnailer.prototype.process1 = function(self, u) {

self.center.x = (u + 0.5) * self.ratio;

self.icenter.x = Math.floor(self.center.x);

for (var v = 0; v < self.dest.height; v++) {

self.center.y = (v + 0.5) * self.ratio;

self.icenter.y = Math.floor(self.center.y);

var a, r, g, b;

a = r = g = b = 0;

for (var i = self.icenter.x - self.range2; i <= self.icenter.x + self.range2; i++) {

if (i < 0 || i >= self.src.width)

continue;

var f_x = Math.floor(1000 * Math.abs(i - self.center.x));

if (!self.cacheLanc[f_x])

self.cacheLanc[f_x] = {};

for (var j = self.icenter.y - self.range2; j <= self.icenter.y + self.range2; j++) {

if (j < 0 || j >= self.src.height)

continue;

var f_y = Math.floor(1000 * Math.abs(j - self.center.y));

if (self.cacheLanc[f_x][f_y] == undefined)

self.cacheLanc[f_x][f_y] = self.lanczos(Math.sqrt(Math.pow(f_x * self.rcp_ratio, 2)

+ Math.pow(f_y * self.rcp_ratio, 2)) / 1000);

weight = self.cacheLanc[f_x][f_y];

if (weight > 0) {

var idx = (j * self.src.width + i) * 4;

a += weight;

r += weight * self.src.data[idx];

g += weight * self.src.data[idx + 1];

b += weight * self.src.data[idx + 2];

}

}

}

var idx = (v * self.dest.width + u) * 3;

self.dest.data[idx] = r / a;

self.dest.data[idx + 1] = g / a;

self.dest.data[idx + 2] = b / a;

}

if (++u < self.dest.width)

setTimeout(self.process1, 0, self, u);

else

setTimeout(self.process2, 0, self);

};

thumbnailer.prototype.process2 = function(self) {

self.canvas.width = self.dest.width;

self.canvas.height = self.dest.height;

self.ctx.drawImage(self.img, 0, 0, self.dest.width, self.dest.height);

self.src = self.ctx.getImageData(0, 0, self.dest.width, self.dest.height);

var idx, idx2;

for (var i = 0; i < self.dest.width; i++) {

for (var j = 0; j < self.dest.height; j++) {

idx = (j * self.dest.width + i) * 3;

idx2 = (j * self.dest.width + i) * 4;

self.src.data[idx2] = self.dest.data[idx];

self.src.data[idx2 + 1] = self.dest.data[idx + 1];

self.src.data[idx2 + 2] = self.dest.data[idx + 2];

}

}

self.ctx.putImageData(self.src, 0, 0);

self.canvas.style.display = "block";

};

…你可以用它產(chǎn)生這樣的結(jié)果!

因此,無論如何,這是示例的“固定”版本:

img.onload = function() {

var canvas = document.createElement("canvas");

new thumbnailer(canvas, img, 188, 3); //this produces lanczos3

// but feel free to raise it up to 8. Your client will appreciate

// that the program makes full use of his machine.

document.body.appendChild(canvas);

};

現(xiàn)在是時候讓最好的瀏覽器進(jìn)站了,看看哪種瀏覽器最有可能增加客戶

總結(jié)

以上是生活随笔為你收集整理的html5图像调整大小,JavaScript调整HTML5画布中图像的大小的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。