HTML5 キャンバスで画像のサイズを変更する 質問する

HTML5 キャンバスで画像のサイズを変更する 質問する

私はJavaScriptとキャンバス要素を使ってクライアント側でサムネイル画像を作成しようとしていますが、画像を縮小すると見栄えが悪くなります。まるでPhotoshopで縮小し、リサンプリングをバイキュービックではなく「ニアレストネイバー」に設定したように見えます。これを正しく表示することは可能だとわかっています。このサイトキャンバスを使用しても問題なく実行できます。[ソース] リンクに示されているのと同じコードを使用してみましたが、それでも見栄えがひどいです。何か見落としている点や、設定する必要がある点などがあるのでしょうか?

編集:

jpg のサイズを変更しようとしています。リンクされたサイトと Photoshop で同じ jpg のサイズを変更してみましたが、縮小すると問題なく表示されます。

関連するコードは次のとおりです。

reader.onloadend = function(e)
{
    var img = new Image();
    var ctx = canvas.getContext("2d");
    var canvasCopy = document.createElement("canvas");
    var copyContext = canvasCopy.getContext("2d");

    img.onload = function()
    {
        var ratio = 1;

        if(img.width > maxWidth)
            ratio = maxWidth / img.width;
        else if(img.height > maxHeight)
            ratio = maxHeight / img.height;

        canvasCopy.width = img.width;
        canvasCopy.height = img.height;
        copyContext.drawImage(img, 0, 0);

        canvas.width = img.width * ratio;
        canvas.height = img.height * ratio;
        ctx.drawImage(canvasCopy, 0, 0, canvasCopy.width, canvasCopy.height, 0, 0, canvas.width, canvas.height);
    };

    img.src = reader.result;
}

編集2:

どうやら私が間違っていたようです。リンク先のウェブサイトでは、画像の縮小がうまくいっていませんでした。提案された他の方法も試してみましたが、どれも見栄えがよくありませんでした。さまざまな方法の結果は次のとおりです。

フォトショップ:

代替テキスト

キャンバス:

代替テキスト

画像レンダリング: optimizeQuality が設定され、幅/高さでスケーリングされた画像:

代替テキスト

image-rendering: optimizeQuality が設定され、-moz-transform でスケーリングされた画像:

代替テキスト

pixastic でのキャンバスのサイズ変更:

代替テキスト

これは、Firefox が本来の目的どおりにバイキュービック サンプリングを使用していないことを意味していると思います。実際に追加されるまで待つしかありません。

編集3:

オリジナル画像

ベストアンサー1

では、すべてのブラウザ (実際、Chrome 5 はかなり良い品質でした) で十分なリサンプリング品質が得られない場合はどうすればいいでしょうか。それなら自分で実装しましょう。さあ、Web 3.0、HTML5 準拠のブラウザ、超最適化された JIT JavaScript コンパイラ、マルチコア (†) マシン、大量のメモリを備えた新しい時代に入りつつあるのですから、何を恐れているのでしょう。JavaScript には「Java」という単語が含まれているので、パフォーマンスは保証されるはずですよね。サムネイル生成コードをご覧ください。

// 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";
};

...これにより、このような結果を生み出すことができます。

img717.imageshack.us/img717/8910/lanczos358.png

とにかく、ここにあなたの例の「修正された」バージョンがあります:

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);
};

今度は、最高のブラウザを比較して、どれがクライアントの血圧を上げる可能性が最も低いかを調べてみましょう。

えーと、私の皮肉タグはどこにあるのでしょうか?

(コードの多くの部分はAnrieff ギャラリージェネレーターGPL2 にも含まれていますか? わかりません)

実際には、JavaScript の制限により、マルチコアはサポートされていません。

おすすめ記事