Advertisement
Guest User

Untitled

a guest
Nov 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. import { Component, OnInit, AfterContentInit, AfterViewInit, Input } from '@angular/core';
  2. import * as interact from 'interactjs';
  3.  
  4.  
  5. @Component({
  6. selector: 'app-canvas-object',
  7. templateUrl: './canvas-object.component.html',
  8. styleUrls: ['./canvas-object.component.scss']
  9. })
  10. export class CanvasObjectComponent implements AfterViewInit {
  11.  
  12. @Input() parentCanvas: HTMLElement;
  13.  
  14. thisRect: HTMLElement;
  15.  
  16. constructor() { }
  17.  
  18. test() {
  19. console.log('hello wolrd');
  20. }
  21.  
  22. ngAfterViewInit() {
  23.  
  24. this.thisRect = document.getElementById('myRect');
  25. this.thisRect.style.background = 'red';
  26.  
  27.  
  28. interact('.resize-drag')
  29. .draggable({
  30. onmove: this.dragMoveListener,
  31. restrict: {
  32. restriction: this.parentCanvas.getBoundingClientRect(),
  33. elementRect: { top: 0, left: 0, bottom: 1, right: 1 },
  34.  
  35. },
  36.  
  37.  
  38. })
  39. .resizable({
  40. // resize from all edges and corners
  41. edges: { left: false, right: true, bottom: true, top: false },
  42.  
  43. // keep the edges inside the parent
  44. restrictEdges: {
  45. outer: this.parentCanvas.getBoundingClientRect(),
  46. endOnly: true
  47. },
  48.  
  49. // minimum size
  50. restrictSize: {
  51. min: { width: 100, height: 50 },
  52. },
  53.  
  54. inertia: true,
  55. })
  56. .on('resizemove', function (event) {
  57.  
  58. console.log(event);
  59.  
  60.  
  61. let target = event.target,
  62. x = (parseFloat(target.getAttribute('data-x')) || 0),
  63. y = (parseFloat(target.getAttribute('data-y')) || 0);
  64.  
  65. // update the element's style
  66.  
  67. target.style.width = event.rect.width + 'px';
  68. target.style.height = event.rect.height + 'px';
  69.  
  70. // translate when resizing from top or left edges
  71.  
  72.  
  73. x += event.deltaRect.left;
  74. y += event.deltaRect.top;
  75.  
  76. target.style.webkitTransform = target.style.transform =
  77. 'translate(' + x + 'px,' + y + 'px)';
  78.  
  79. target.setAttribute('data-x', x);
  80. target.setAttribute('data-y', y);
  81. });
  82. }
  83.  
  84. dragMoveListener (event) {
  85. const target = event.target,
  86. // keep the dragged position in the data-x/data-y attributes
  87. x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
  88. y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
  89.  
  90. // translate the element
  91. target.style.webkitTransform =
  92. target.style.transform =
  93. 'translate(' + x + 'px, ' + y + 'px)';
  94.  
  95. // update the posiion attributes
  96. target.setAttribute('data-x', x);
  97. target.setAttribute('data-y', y);
  98. }
  99.  
  100.  
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement