Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can magnetjs be used with interactjs resize? #14

Open
talhakaanozkan opened this issue Dec 23, 2019 · 7 comments
Open

Can magnetjs be used with interactjs resize? #14

talhakaanozkan opened this issue Dec 23, 2019 · 7 comments
Assignees
Labels
bug Something goes wrong feature New feature

Comments

@talhakaanozkan
Copy link

https://github.com/taye/interact.js

hi, i'm using interactjs in events like resize. But how do you use magnetjs (snap events) to make the two better together?

drag event works with magnet. I couldn't run with the resize event. can you help me ?

@lf2com lf2com added this to the dragging and resizing milestone Dec 24, 2019
@lf2com lf2com added feature New feature question Need more information labels Dec 24, 2019
@lf2com lf2com self-assigned this Dec 24, 2019
@lf2com
Copy link
Owner

lf2com commented Dec 24, 2019

Hi @talhakaanozkan, it sounds like you want to apply magnet effects when resizing. Unfortunately magnet.js does not support this function. However since interact.js already integrate both dragging and resizing together, it is a good reference to migrate into magnet.js to doing attracts when resizing. I will add this into the next milestone.
Currently we need to implement it ourselves with magnet.check(element, rect) and magnet.setMemberRectangle(element, rect).
I wrote a sample to manually check alignments of rightToLeft, leftToRight, topToBottom, and bottomToTop then apply position/size changes:

const magnet = new Magnet();
const handleChange = (evt, fixedSize = false) => {
  const { dx, dy, rect: currRect, target } = evt;
  const checkResult = magnet.check(target, currRect, ['rightToLeft', 'leftToRight', 'topToBottom', 'bottomToTop']);
  const { mins } = checkResult;
  const resultRect = {
    top: currRect.top,
    left: currRect.left,
    width: currRect.width,
    height: currRect.height,
  };
  const distance = magnet.getDistance();
  const getMinimum = (types) => {
    return types
      .reduce((result, type, typeIndex) => {
        const target = mins[type];
        if (type === target.min) {
          const currValue = target.results[type];
          if (currValue < result.value) {
            return {
              value: currValue,
              type,
              dom: target.target,
            };
          }
        }
        return result;
      }, {
        value: Infinity,
        type: null,
        dom: null,
      });
  };
  if (dx) {
    let { value, type, dom } = getMinimum(['rightToLeft', 'leftToRight']);
    if (value <= distance) {
      let { rect } = dom;
      switch (type) {
        case 'rightToLeft':
          if (fixedSize) {
            resultRect.left = (rect.left - resultRect.width);
          } else {
            resultRect.width = (rect.left - currRect.left);
          }
          break;
        case 'leftToRight':
          resultRect.left = rect.right;
          if (!fixedSize) {
            resultRect.width = (currRect.right - rect.right);
          }
          break;
      }
    }
  }
  if (dy) {
    let { value, type, dom } = getMinimum(['topToBottom', 'bottomToTop']);
    if (value <= distance) {
      let { rect } = dom;
      switch (type) {
        case 'topToBottom':
          resultRect.top = rect.bottom;
          if (!fixedSize) {
            resultRect.height = (currRect.bottom - rect.bottom);
          }
          break;
        case 'bottomToTop':
          if (fixedSize) {
            resultRect.top = (rect.top - resultRect.height);
          } else {
            resultRect.height = (rect.top - currRect.top);
          }
          break;
      }
    }
  }
  magnet.setMemberRectangle(target, Magnet.stdRect(resultRect));
};

magnet.add(element);
magnet.setAllowDrag(false); // need to disable dragging function of magnet.js
interact(element)
  .draggable({
    listeners: {
      move: (evt) => handleChange(evt, true),
    },
  })
  .resizable({
    edges: { top: true, right: true, bottom: true, left: true },
    onmove: (evt) => handleChange(evt, false),
  });

@talhakaanozkan
Copy link
Author

thanks for your quick answer

@talhakaanozkan
Copy link
Author

image

Where does the value "mins" always be empty?

@lf2com
Copy link
Owner

lf2com commented Dec 24, 2019

Sorry, That's my mistake that I didn't realize you were using jQuery plugin. After repeating what you encountered, I found out there were some functions of magnet.js did not really return value for jQuery plugin. I fixed this issue and released a new version here. Also I have tested it on the code and it worked. Please use the v1.0.6 version of magnet.js jQuery plugin.

@talhakaanozkan
Copy link
Author

I'm trying now.

@talhakaanozkan
Copy link
Author

talhakaanozkan commented Dec 24, 2019

thanks worked.

@lf2com
Copy link
Owner

lf2com commented Dec 25, 2019

Glade to hear that! If there is any question, please kindly let me know.

@lf2com lf2com added bug Something goes wrong and removed question Need more information labels Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something goes wrong feature New feature
Projects
None yet
Development

No branches or pull requests

2 participants