gpt4 book ai didi

javascript - 获取宽度和高度的更新

转载 作者:行者123 更新时间:2023-11-30 14:57:19 28 4
gpt4 key购买 nike

我可以更新显示在 HTML 上的矩形的宽度和高度吗?因为 rect 可以缩放宽度和高度所以我也希望它更新但我不知道如何更新。有什么解决办法吗?

<html>
<canvas id="c" width="900" height="720"></canvas>
<p id="show"></p>
<button onclick="addRect2()">Rect</button>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.19/fabric.js"></script>

</html>

JS

var width;
var height;
var canvas = new fabric.Canvas('c');
window.addRect2 = function(width,height){
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'black',
width: 250,
height: 250,
});
canvas.add(rect);
width = rect.width;
height = rect.height;
document.getElementById("show").innerHTML = "Width and Height" + width " " + height;}

最佳答案

var width,height;
var canvas = new fabric.Canvas('c');
var rect = new fabric.Rect({
left: 100,
top: 100,
fill: 'black',
width: 250,
height: 250,
});
canvas.add(rect);
var el = document.getElementById("show");
displayDim();

rect.on('modified',function(){
displayDim();
})

rect.on('scaling',function(){
displayDim();
})

function displayDim(){
width = parseInt(rect.width * rect.scaleX);
height = parseInt(rect.height * rect.scaleY);
el.innerHTML = "Width: " + width + "px and Height: " + height+"px";
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.19/fabric.js"></script>
<canvas id="c" width="500" height="400"></canvas>
<p id="show"></p>

使用events , 而 scaling/modified 更新宽度/高度。要得到缩放后的高/宽,需要分别乘以scaleY/scaleX。

关于javascript - 获取宽度和高度的更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47048564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com