I have a Problem on Konva, this script draw some point and lines between the point and dragable i want to select the lines oder points and when i hover or click on the created line/point i have an error eg: Uncaught TypeError: Cannot read properties of undefined (reading 'newLine')
function updateObjects() {
var shapes = stage.find('.line');
shapes.forEach((shape) => {
shape.destroy();
})
targets.forEach((target) => {
(function () {
if (targets.length > 1) {
if (target.id != targets.length - 1) {
const targid = '#targets' + target.id;
const targid2 = '#targets' + (target.id + 1);
var ctarg = stage.find(targid)[0];
var entarg = stage.find(targid2)[0];
if (ctarg != null && entarg != null) {
var newLine = new Konva.Line({
points: [ctarg.x(), ctarg.y(), entarg.x(), entarg.y()],
stroke: 'black',
strokeWidth: 5,
lineCap: 'round',
lineJoin: 'round',
id: 'lineId=' + idCounter,
name: 'line',
});
idCounter++;
//become an error on THIS
this.newLine.on('mouseover', () => {
document.body.style.cursor = 'pointer';
this.strokeWidth(12);
});
layerLines.add(newLine);
stage.add(layerLines);
stage.add(layer);
}
}
}
})();
});
}
on the this.strokeWidth(xx); i have en error
Simple scope bug in your code - newline r=declared as
var newLine
then refered to as
this.newLine
Maybe you made a mistake preparing your code sample to post ?