サイズが300x300の固定HTML5キャンバスがあります。ページ上の他の画像のサイズを変更して、JavaScriptを使用して最大幅/高さを300px(より大きなサイズに応じて)にしています。Javascript:固定サイズのキャンバスで可変サイズの画像
これはソースイメージのサイズを実際には変更しません。キャンバスに描画すると、元のサイズが使用されます。
- それらが300ピクセルの最大の高さや幅をキャンバスに描かれているように、JavaScriptを使用して、これらの画像のサイズを変更:
がする方法があります。
AND
- 可変サイズの画像と300×300の正方形を完全に内部に含まれるようにキャンバスは、その内部リサイズ画像との300×300の寸法を保持持ちそれは作成されますか?
(あなたは様々な画像にドロップすると、彼らが収まるようにサイズを変更300×300 Photoshopファイルを作る考えて)
はJavascriptを/キャンバスでさえも可能、このですか?ここで
EDIT
は、私は受け入れ答えに基づいて使用して終了コードです:
var ssItems = [exampleUrls], //array of image paths
imgHolder = document.getElementById("img-holder");
for (var i = 0; i < ssItems.length; i++) {
var img = new Image(),
imgSrc = "/ItemImages/Large/" + ssItems[i] + ".jpg",
imgW,
imgH;
img.src = imgSrc;
img.onload = function() {
var canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
canvas.setAttribute("class", "img-canvas");
canvas.setAttribute("width", "300");
canvas.setAttribute("height", "300");
imgHolder.appendChild(canvas);
imgW = (canvas.height * this.width)/this.height;
imgH = (canvas.width * this.height)/this.width;
if (this.height > this.width) {
ctx.drawImage(this, canvas.width/2 - imgW/2, 0, imgW, canvas.height);
} else {
ctx.drawImage(this, 0, canvas.height/2 - imgH/2, canvas.width, imgH);
}
}
あなたは次のように意味するか、[この](https://stackoverflow.com/questions/21961839/simulation-background-size-cover-in-canvas/21961894#21961894)、[this](https://stackoverflow.com/questions/14087483/how-to-proportionally-resize-an-image-in-canvas/14087607#14087607)のようなものですか? – K3N
または[SVGスタイル](http://stackoverflow.com/questions/34428723/how-to-implement-svgs-preserveaspectratio-xminymin-slice-for-canvas-drawimage/34429774#34429774)? – Kaiido
@ K3Nは似ていますが、例では 'background-size:cover'や' zoom 'エフェクトのように見えます。しかし、ブログ投稿は役に立ちました。 – Tier