Git Product home page Git Product logo

Comments (3)

akira-cn avatar akira-cn commented on May 28, 2024

直接transform layer元素就行了吧

from spritejs.

2ming avatar 2ming commented on May 28, 2024

我试一下,其实我是想实现一个画板的类似功能,需要放大,缩小,拖动元素,大概写了一些,感觉不太方便
layer上面有一个scale属性,可以放大缩小,但是放大或者缩小后的元素pos偏移的x,y有点对应不上,我按照放大的比例去做一个计算还是有些问题

下面是我简单写的一个实例

import * as spritejs from "spritejs";
const { Scene, Path, Group, SpriteSvg } = spritejs;

export function drawScene(container, options = {}) {
  //获取容器的宽高
  const { width, height } = container.getBoundingClientRect();
  debugger;
  const scene = new Scene({
    container,
    mode: "stickyTop",
    width,
    height,
    ...options,
  });
  const layer = scene.layer({
    // handleEvent: false,
  });

  const background = new Group({
    width,
    height,
  });
  layer.append(background);
  const eventStore = {
    //当前按住的按键
    keyCode: null,
    //当前选中元素
    parts: null,
    scale: 1,
    //鼠标按下的位置
    offsetX: 0,
    offsetY: 0,
    mouseX: 0,
    mouseY: 0,
    moveEvt: null
  };

  layer.attr({
    scale: [1, 1],
    // bgcolor: "white",
    borderWidth: 1,
  });

  function getViewport(val) {
    return val*eventStore.scale
  }
                      
  scene.addEventListener("mousedown", (evt) => {
    const { target, x, y } = evt;
    eventStore.mouseX = x;
    eventStore.mouseY = y;
    console.log(evt);
    if (target.attributes.type == "path") {
      eventStore.parts = evt.target;
    }
  });
  // document.addEventListener("mouseup", (evt) => {

  // })
  document.addEventListener("mouseup", (evt) => {
    const { mouseX, mouseY, keyCode, offsetX, offsetY, moveEvt} = eventStore;
    const { x, y } = moveEvt || {};
    console.log("====================");
    //重置选中元素
    eventStore.parts = null;

    //记录画布偏移的位置
    if ((mouseX || mouseY) && keyCode == 32) {
      
      eventStore.offsetX =offsetX + x - mouseX;
      eventStore.offsetY =offsetY + y - mouseY;
      background.attr({
        pos: [eventStore.offsetX, eventStore.offsetY],
      });

    }
    eventStore.keyCode = null;

    //重置鼠标的位置
    eventStore.mouseX = 0;
    eventStore.mouseY = 0;
  });

  scene.addEventListener("mousemove", (evt) => {
    const { x, y } = evt;
    const { mouseX, mouseY, offsetX, offsetY } = eventStore;

    if (eventStore.keyCode == 32 && mouseX && mouseY) {
      eventStore.moveEvt = evt;
      background.attr({
        // scale: [1, 1],
        pos: [offsetX + x - mouseX, offsetY + y - mouseY],
        // x: offsetX + x - mouseX,
        // y: offsetY + y - mouseY
      });
      // scene.attr({
      //   scale: [1, 1],
      //   pos: [eventStore.offsetX, eventStore.offsetY]
      // });
      // layer.getOffsetPosition(offsetX + x - mouseX, offsetY + y - mouseY)
    } else if (eventStore.parts) {
      // evt.stopPropagation();
      eventStore.parts.attr("pos", [x - offsetX, y - offsetY]);
    }
  });

  document.addEventListener("keydown", (evt) => {
    evt.preventDefault();
    const { keyCode } = evt;
    eventStore.keyCode = keyCode;
    eventStore.parts = null
  });

  document.addEventListener("keyup", (evt) => {
    evt.preventDefault();
    eventStore.keyCode = null;
    //重置鼠标的位置
    eventStore.mouseX = 0;
    eventStore.mouseY = 0;
  });
  document.addEventListener("wheel", (evt) => {
    evt.preventDefault();
    if (eventStore.keyCode == 32){                                                                         
      console.log(eventStore.scale, evt)
      eventStore.scale += evt.deltaY * -0.001;
      eventStore.offsetX = evt.deltaY * -0.001;
      eventStore.offsetY = evt.deltaY * -0.001;
  
      // Restrict scale
      eventStore.scale = Math.min(Math.max(0.125, eventStore.scale), 4);
      console.log(eventStore.scale)
      background.attr({
        scale: [eventStore.scale, eventStore.scale],
      });
    }
  });

  return {
    layer: background,
    scene,
  };
}

from spritejs.

akira-cn avatar akira-cn commented on May 28, 2024

你先设置anchor属性或者transform-origin 属性,然后再缩放,transform-origin 就是缩放的原点,这个和css操作dom元素是一样的。

from spritejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.