当我上传图片到konva.js时,是否有办法保持图像的比例。基本上,我使用konva和Vue扩展,我需要这样的方法上传背景图像。我已经设置了基于用户设备的画布大小,我可以使上传图像大小到画布尺寸,但它不会节省它的比例。
我认为,我可以通过编写函数来确定图像的最大高度或宽度(在特定情况下选择哪个更合适),然后按比例调整它的大小,但我不认为这个答案是优雅的和没有bug的,所以我想在做之前我会先问一下。
发布于 2018-12-02 15:56:07
您需要自己处理图像缩放,但这并不困难。
正如您已经确定的,您需要一个函数来设置图像的比例,以覆盖视口,同时保留高宽比。
所需的数学方法是计算视口宽度与图像宽度的比率,以及高度相同的比例。然后比较这两种比率,并使用较大的。
除非视口和图像的纵横比完全相同,否则当选择300x500按钮时,会发现一些图像被剪裁在片段中。
下面的片段显示了表示一个视点的粉红色矩形。维度按钮改变视口比例,宽度x高度按钮在不同的图像中调用。通过使用按钮,您可以看到拟合功能的工作原理。
实验1:开始时,重复点击“更宽”按钮,看看图片的顶部和底部是如何在视口外剪裁的。
实验2:在开始时,重复单击“窄”,观察相似的效果。
/*
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');<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>
https://stackoverflow.com/questions/53526737
复制相似问题