首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >上传时保持图像比率

问上传时保持图像比率
EN

Stack Overflow用户
提问于 2018-11-28 19:28:23
回答 1查看 545关注 0票数 0

当我上传图片到konva.js时,是否有办法保持图像的比例。基本上,我使用konva和Vue扩展,我需要这样的方法上传背景图像。我已经设置了基于用户设备的画布大小,我可以使上传图像大小到画布尺寸,但它不会节省它的比例。

我认为,我可以通过编写函数来确定图像的最大高度或宽度(在特定情况下选择哪个更合适),然后按比例调整它的大小,但我不认为这个答案是优雅的和没有bug的,所以我想在做之前我会先问一下。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-02 15:56:07

您需要自己处理图像缩放,但这并不困难。

正如您已经确定的,您需要一个函数来设置图像的比例,以覆盖视口,同时保留高宽比。

所需的数学方法是计算视口宽度与图像宽度的比率,以及高度相同的比例。然后比较这两种比率,并使用较大的。

除非视口和图像的纵横比完全相同,否则当选择300x500按钮时,会发现一些图像被剪裁在片段中。

下面的片段显示了表示一个视点的粉红色矩形。维度按钮改变视口比例,宽度x高度按钮在不同的图像中调用。通过使用按钮,您可以看到拟合功能的工作原理。

实验1:开始时,重复点击“更宽”按钮,看看图片的顶部和底部是如何在视口外剪裁的。

实验2:在开始时,重复单击“窄”,观察相似的效果。

​

代码语言:javascript
复制
/*
function to caluclate and return appropriate scale to fill one rect with another whilst preserving aspect ratio
*/
var autoScale = function(container, imgEle){

var rW = container.width() / imgEle.width;
var rH = container.height() / imgEle.height;

var scale = (rW < rH ? rH : rW);
return {x: scale, y: scale};
}

// from here on the code is about making the demo.

var sz = '600x600';
var miniMag = 0.3333

// Set up the canvas / stage
var div = $('#container');

var stage = new Konva.Stage({container: 'container', width: div.width(), height: div.height()});
var layer = new Konva.Layer({draggable: false});
stage.add(layer)
stage.scale({x: miniMag, y: miniMag});
var pic = new Konva.Image({ x: 300, y: 300});
layer.add(pic);
var rect = new Konva.Rect({x: 300, y: 300, width : div.width(), height: div.height(), stroke: 'magenta'})
layer.add(rect)
stage.draw()

// load an image
var imageObj = new Image();
imageObj.onload = function(){

  pic.image(imageObj);  
  pic.scale(autoScale(rect, imageObj));
  pic.x(rect.x() + ((rect.width() -  (pic.width() * pic.scaleX()) )/2)) 
  pic.y(rect.y() + ((rect.height() -  (pic.height() * pic.scaleY()) )/2)) 
  layer.draw();

}

// if we click a change-size button then change the viewport indicator
$('#narrower').data('change', {x:-10, y:   0});
$('#wider').data('change',  {x: 10, y:   0});
$('#shorter').data('change', {x:  0, y: -10});
$('#taller').data('change',  {x:  0, y:  10});
$('.btn').on('click', function(e){

  var diff = $(this).data('change');  
  rect.width(rect.width() + diff.x)
  rect.height(rect.height() + diff.y)
  pic.scale(autoScale(rect, imageObj));
  pic.x(rect.x() + ((rect.width() -  (pic.width() * pic.scaleX()) )/2)) 
  pic.y(rect.y() + ((rect.height() -  (pic.height() * pic.scaleY()) )/2)) 
  layer.draw();

})

// If we click an image selection button change the image
$('.imgsel').on('click', function(e){

imageObj.src = "https://via.placeholder.com/" + $(this).attr('sz');

})

// Kick off with a 600 x 600 image
$('.imgsel600').trigger('click');
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/2.5.1/konva.min.js"></script>
<div>
<button id='narrower' class='btn'>Narrower</button>
<button id='wider' class='btn'>Wider</button>
<button id='shorter' class='btn'>Shorter</button>
<button id='taller' class='btn'>Taller</button>
<button class='imgsel' sz='300x300'>300 x 300</button>
<button class='imgsel' sz='300x500'>300 x 500</button>
<button class='imgsel' sz='500x300'>500 x 300</button>
<button class='imgsel imgsel600' sz='600x600'>600 x 600</button>
</div>
<div id='container' style="position: absolute; z-index: -1; display: inline-block; left: 0px; top: 0px; width: 300px; height: 300px; background-color: silver;"></div>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53526737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档