vue-konva
vue-konva copied to clipboard
Very nice Library ! Trying to convert following example to vuejs:
var width = 960;
var height = 600;
var stage = new Konva.Stage({
container: 'container',
width: width,
height: height,
});
var layer = new Konva.Layer();
stage.add(layer);
let currentShape;
document.getElementById('Rect').addEventListener("click", function () {
let item = new Konva.Rect({
x: 20,
y: 20,
width: 100,
height: 50,
fill: 'green',
stroke: 'black',
strokeWidth: 4,
draggable: true,
});
var tr = new Konva.Transformer();
layer.add(tr);
layer.add(item);
tr.nodes([item]);
layer.draw();
});
var menuNode = document.getElementById('menu');
document.getElementById('delete-button').addEventListener('click', () => {
var tr = layer.find('Transformer').find(tr => tr.nodes()[0] === currentShape);
tr.destroy();
currentShape.destroy();
layer.draw();
});
window.addEventListener('click', () => {
menuNode.style.display = 'none';
});
stage.on('contextmenu', function (e) {
e.evt.preventDefault();
if (e.target === stage) {
return;
}
currentShape = e.target;
menuNode.style.display = 'initial';
var containerRect = stage.container().getBoundingClientRect();
menuNode.style.top =
containerRect.top + stage.getPointerPosition().y + 4 + 'px';
menuNode.style.left =
containerRect.left + stage.getPointerPosition().x + 4 + 'px';
});
What did you try?
JavaScirpt link : https://codesandbox.io/s/wild-sky-zddko?file=/index.html
Vue ,I tried it like this. But I want to try like that JavaScript.
Vue link : https://codesandbox.io/s/testpage-ign4f?file=/src/testPage_vuex.vue
I want to add this function. (I'd like to do the same thing as possible.)
- delete button click , why not this.currentShape target undefined ??
why not this.currentShape target undefined ??
Because this
is not what you expect. You need to learn more about contexts in js. In should, you need to use arrow functions instead of function() {}