当鼠标悬停在像这个视频这样的对象上时,我想显示对象边界框,怎么做?
我将canvas.on('mouse:over')与selectedObj.drawBorders函数一起使用。但是,轮廓框绘制在不正确的位置。而且我不知道当鼠标移出对象时如何清除该轮廓框。
这是我的代码:
$(function() {
var canvasObject = document.getElementById("editorCanvas");
// set canvas equal size with div
$(canvasObject).width($("#canvasContainer").width());
$(canvasObject).height($("#canvasContainer").height());
var canvas = new fabric.Canvas('editorCanvas', {
backgroundColor: 'white',
selectionLineWidth: 2,
width: $("#canvasContainer").width(),
height: $("#canvasContainer").height()
});
canvas.viewportTransform[4] = 20;
canvas.viewportTransform[5] = 40;
canvas.on('mouse:over', function(opts) {
var selectedObj = opts.target;
if (selectedObj != null) {
selectedObj.drawBorders(canvas.getContext())
}
});
var text = new fabric.Text('hello world', { left: 50, top: 50 });
canvas.add(text);
setObjectCoords();
function setObjectCoords() {
canvas.forEachObject(function(object) {
object.setCoords();
});
}
});
<style>
#canvasContainer {
width: 100%;
height: 100vh;
background-color: gray;
}
</style>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.4.3/fabric.js"></script>
<div id="canvasContainer">
<canvas id="editorCanvas"></canvas>
</div>
请帮我解决这个问题!
谢谢!