konva_demo

by tobieski

HTML

<!DOCTYPE html>
<html>
  <head>
    <script src="https://unpkg.com/konva@9.3.6/konva.min.js"></script>
    <meta charset="utf-8" />
    <title>Konva Drag and Drop Demo</title>
    <style>
      body {
        margin: 0;
        padding: 0;
        overflow: hidden;
        background-color: #f0f0f0;
      }
    </style>
  </head>
  <body>
    <div id="buttons-layout">
      <button onclick="addRectangle()">Add rectangle</button>
      <button onclick="addImage()">Add image</button>
      <button onclick="add500Images()">Add 500 images</button>
      <button onclick="addFrame()">Add frame</button>
      <button onclick="addGroup()">Add group</button>
    </div>
    <div id="container"></div>
    <script>
      const width = window.innerWidth;
      const height = window.innerHeight;

      const stage = new Konva.Stage({
        container: 'container',
        width: width,
        height: height,
      });

      const layer = new Konva.Layer();
      stage.add(layer);

      Konva.Image.fromURL(
      'https://fastly.picsum.photos/id/977/200/300.jpg?hmac=YYtcm39X8v9y0KYAb_9s-ufIz_R0Kgbt_EP0F8-jkFU',
      (img) => {
        img.setAttrs({
          width: 300,
          height: 100,
          x: 80,
          y: 100,
          name: 'selectable',
          draggable: true,
          keepRatio: true
        });

        layer.add(img);
      });

      Konva.Image.fromURL(
        'https://fastly.picsum.photos/id/877/200/200.jpg?hmac=_TafLl6WZhZyakFIYbCsHjkQbQ_aQreI80z-xcPFQIg',
        (img) => {
          img.setAttrs({
            width: 250,
            height: 250,
            x: 80,
            y: 100,
            name: 'selectable',
            draggable: true,
            keepRatio: true
          });

          layer.add(img);
      });

      const frame = new Konva.Rect({
        x:250,
        y:250,
        width: 250,
        height: 250,
        stroke: 'black',
        draggable: true,
        name: 'selectable'
      });
      layer.add(frame);

...