Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. var imgURL = 'http://fabricjs.com/lib/pug.jpg';
  2. var clipYellowRect = null;
  3. var clipRedRect = null;
  4. var pug = null;
  5. var canvas = new fabric.Canvas('c');
  6.  
  7. // insert image into canvas
  8. var pugImg = new Image();
  9. pugImg.onload = function (img) {
  10. pug = new fabric.Image(pugImg, {
  11. angle: 0,
  12. width: 500,
  13. height: 500,
  14. left: 100,
  15. top: 50,
  16. scaleX: 0.5,
  17. scaleY: 0.5,
  18. clipName: 'pug',
  19. });
  20. canvas.add(pug);
  21. };
  22. pugImg.src = imgURL;
  23.  
  24. //draw yellow rectangle
  25. $('#btnYellowRect').on('click', function(){
  26. clipYellowRect = new fabric.Rect({
  27. originX: 'left',
  28. originY: 'top',
  29. left: 120,
  30. top: 60,
  31. width: 200,
  32. height: 200,
  33. fill: 'rgba(255,255,0,0.5)',
  34. strokeWidth: 0,
  35. selectable: false
  36. });
  37. canvas.add(clipYellowRect);
  38. });
  39.  
  40. //draw red rectangle
  41. $('#btnRedRect').on('click', function(){
  42. clipRedRect = new fabric.Rect({
  43. originX: 'left',
  44. originY: 'top',
  45. left: 90,
  46. top: 120,
  47. width: 100,
  48. height: 100,
  49. strokeWidth: 3,
  50. fill: 'transparent',
  51. stroke: 'rgba(255,0,0,1)', /* use transparent for no fill */
  52. strokeWidth: 0,
  53. selectable: false
  54. });
  55.  
  56. canvas.add(clipRedRect);
  57. });
  58.  
  59. //clip
  60. $('#btnClip').on('click', function(){
  61. var yellowRectRegion = getRegion(clipYellowRect);
  62. var redRectRegion = getRegion(clipRedRect);
  63. //determine inersection
  64. var intersectResult = PolyBool.intersect({
  65. regions: [yellowRectRegion],
  66. inverted: false
  67. }, {
  68. regions: [redRectRegion],
  69. inverted: false
  70. });
  71.  
  72. //generate clipping path
  73. var xorResult = PolyBool.xor({
  74. regions: [yellowRectRegion],
  75. inverted: false
  76. }, {
  77. regions: intersectResult.regions,
  78. inverted: false
  79. });
  80. clipImage(xorResult.regions[0]);
  81. });
  82.  
  83. //prepare data for clipping library
  84. function getRegion(rect){
  85. return [[rect.left, rect.top],
  86. [rect.left + rect.width, rect.top],
  87. [rect.left + rect.width, rect.top + rect.height],
  88. [rect.left, rect.top + rect.height]]
  89. }
  90.  
  91.  
  92. function clipImage(points){
  93. //actual clipping
  94. pug.clipTo = function (ctx) {
  95. var scaleXTo1 = (1 / pug.scaleX);
  96. var scaleYTo1 = (1 / pug.scaleY);
  97. ctx.save();
  98.  
  99. var ctxLeft = -( pug.width / 2 );
  100. var ctxTop = -( pug.height / 2 );
  101.  
  102. ctx.translate( ctxLeft, ctxTop );
  103. ctx.scale(scaleXTo1, scaleYTo1);
  104. ctx.beginPath();
  105. console.log(points)
  106. ctx.moveTo(points[0][0] - pug.oCoords.tl.x, points[0][1] - pug.oCoords.tl.y);
  107. for (var i=1; i < points.length; i++){
  108. ctx.lineTo(points[i][0] - pug.oCoords.tl.x, points[i][1] - pug.oCoords.tl.y);
  109. }
  110. ctx.closePath();
  111. ctx.restore();
  112. };
  113. clipYellowRect.remove();
  114. clipRedRect.remove();
  115. canvas.renderAll();
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement