Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clip to drawn path

I'm trying to draw a path and them use it as a mask of my canvas.

'use strict';

var canvas = new fabric.Canvas('c', {
    hoverCursor: 'pointer',
    isDrawingMode: true
});

canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.color = '#000';
canvas.freeDrawingBrush.width = 100;

fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(img) {

    canvas.add(img);
    canvas.setWidth(img.getWidth());
    canvas.setHeight(img.getHeight());
    canvas.centerObject(img);
    img.selectable = false;
});

canvas.on('path:created', function(data) {

    var path = data.path;

    canvas.remove(path);
    canvas.clipTo = function(context) {
        path.render(context);
    };

    canvas.isDrawingMode = false;
    canvas.renderAll();
});

How can I make the entire path be the visible area of my image?

http://jsfiddle.net/db45yhpo/

EDIT

This is what I'm trying to achieve, but using FabricJS.

http://www.createjs.com/demos/easeljs/alphamaskreveal

like image 431
hasser Avatar asked Oct 13 '15 19:10

hasser


People also ask

How do I make a clip path?

CSS clip-path EditorStart by selecting a polygon() — ellipse() or url() -preset. To move the selected point, use mouse, touch or Arrow -keys.

What is clip path in SVG?

The <clipPath> SVG element defines a clipping path, to be used by the clip-path property. A clipping path restricts the region to which paint can be applied. Conceptually, parts of the drawing that lie outside of the region bounded by the clipping path are not drawn.


1 Answers

I have no experience with fabricjs, so there may be a better way of handling this but I would :
Redraw your path on an hidden canvas,
Then create a new fabric.Image() from this hidden canvas,
Set its globalCompositeOperation parameter to `"destination-atop",
Draw it on the original canvas.

var canvas = new fabric.Canvas('c', {
  hoverCursor: 'pointer',
  isDrawingMode: true
});

canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.color = '#000';
canvas.freeDrawingBrush.width = 100;

fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(img) {
  canvas.add(img);
  canvas.setWidth(img.getWidth());
  canvas.setHeight(img.getHeight());
  canvas.centerObject(img);
  img.selectable = false;
});

canvas.on('path:created', function(data) {
  var clipper = document.createElement('canvas');
  clipper.width = canvas.width;
  clipper.height = canvas.height;
  var ctx = clipper.getContext('2d');
  var path = data.path;
  data.path.render(ctx);
  canvas.remove(path);
  canvas.isDrawingMode = false;
  var oImg = new fabric.Image(clipper)
  oImg.globalCompositeOperation = 'destination-atop';
  canvas.add(oImg);
  canvas.renderAll();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="c"></canvas>
like image 126
Kaiido Avatar answered Oct 02 '22 04:10

Kaiido