简体   繁体   English

在画布中绘制方形图像

[英]Draw a square version of image in canvas

I am trying to show previews of uploaded images in a canvas. 我正在尝试在画布中显示上载图像的预览。 The preview should be a square version of image. 预览应该是图像的正方形版本。

What I have: 我有的:

var img = new Image;
img.src = imageSrc;
var c = document.getElementById('file-preview-' + data.response.id);
var ctx = c.getContext("2d");
img.onload = function() {
    ctx.drawImage(img, 0, 0);
};

And the preview: 和预览:

在此处输入图片说明

So how can I draw on the canvas a 150x150 square with maintained ratio cropped version of the image? 那么,如何在画布上以150x150的正方形绘制比例的图像裁切版本呢?

For example for this image: 例如,此图像:

在此处输入图片说明

I should get: 我应该得到:

在此处输入图片说明

Here's a method that yields precisely the result you desire, cropped, resized and centered (with commented out lines that yield a left based cropping instead)... 这是一种可以精确产生所需结果,裁剪,调整大小和居中的方法(带有注释的行改为产生基于左侧的裁剪)...

  var ctx; function init(event) { ctx = document.getElementById('canvas').getContext('2d'); // Your preview is actually 120x120, // but I've stuck with the textual description of // your requirements here. ctx.canvas.width = 150; ctx.canvas.height = 150; loadBackground('http://i.stack.imgur.com/PCKEo.png'); } function loadBackground(path) { var img = new Image(); img.onload = drawBackground; img.src = path; } function drawBackground(event) { var img = event.target; var imgSize = Math.min(img.width, img.height); // The following two lines yield a central based cropping. // They can both be amended to be 0, if you wish it to be // a left based cropped image. var left = (img.width - imgSize) / 2; var top = (img.height - imgSize) / 2; //var left = 0; // If you wish left based cropping instead. //var top = 0; // If you wish left based cropping instead. ctx.drawImage(event.target, left, top, imgSize, imgSize, 0, 0, ctx.canvas.width, ctx.canvas.height); } window.addEventListener('load', init, false); 
 <canvas id="canvas"></canvas> 

Based on information from this question: SO Question 根据此问题的信息: SO问题

I was able to modify what was done to create a square cropped image in canvas: 我能够修改在画布上创建方形裁剪图像的操作:

var img = new Image;
img.src = "http://cdn.bmwblog.com/wp-content/uploads/2016/02/M-Performance-Parts-BMW-M2-3.jpg"; //Or whatever image source you have
var c= document.getElementById('file-preview');
var ctx=c.getContext("2d");

img.onload = function(){
      ctx.drawImage(img, img.width/2, img.height/2, img.width, img.height, 0, 0, img.width, img.height);
};

You just have to modify the arguments you are passing to the drawImage function as well as add some more. 您只需要修改传递给drawImage函数的参数,然后添加更多参数即可。

You can see a fiddle here: https://jsfiddle.net/4d93pkoa/3/ 您可以在这里看到小提琴: https : //jsfiddle.net/4d93pkoa/3/

Remember that the first two parameters specify the image's top left coordinates within the canvas element. 请记住,前两个参数指定了canvas元素内图像的左上角坐标。

Remember to set the width and height of the canvas element in HTML to a square shape. 请记住,将HTML中的canvas元素的宽度和高度设置为正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM