How to use draggable and click separately in kineticjs?

Member

by lily , in category: Javascript , 4 months ago

How to use draggable and click separately in kineticjs?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 4 months ago

@lily 

To use draggable and click separately in KineticJS, you can create a draggable object and add a click event to it separately. Here is an example code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Create a stage
var stage = new Kinetic.Stage({
  container: 'container',
  width: 500,
  height: 500
});

// Create a layer
var layer = new Kinetic.Layer();

// Create a draggable circle
var circle = new Kinetic.Circle({
  x: 100,
  y: 100,
  radius: 50,
  fill: 'red',
  draggable: true
});

// Add the circle to the layer
layer.add(circle);

// Add the layer to the stage
stage.add(layer);

// Add click event to the circle
circle.on('click', function() {
  alert('Circle clicked!');
});

// Redraw the layer
layer.draw();


In this code, a draggable circle is created and added to the layer. Then a click event is added to the circle separately. When the circle is clicked, an alert box will pop up with the message 'Circle clicked!'. This way, you can use draggable and click separately in KineticJS.