Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fabric.js drawings

(function() {
  var $ = function(id){return document.getElementById(id)};

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

  fabric.Object.prototype.transparentCorners = false;

  var drawingModeEl = $('drawing-mode'),
      drawingOptionsEl = $('drawing-mode-options'),
      drawingColorEl = $('drawing-color'),
      drawingShadowColorEl = $('drawing-shadow-color'),
      drawingLineWidthEl = $('drawing-line-width'),
      drawingShadowWidth = $('drawing-shadow-width'),
      drawingShadowOffset = $('drawing-shadow-offset'),
      clearEl = $('clear-canvas');

  clearEl.onclick = function() { canvas.clear() };

  drawingModeEl.onclick = function() {
    canvas.isDrawingMode = !canvas.isDrawingMode;
    if (canvas.isDrawingMode) {
      drawingModeEl.innerHTML = 'Cancel drawing mode';
      drawingOptionsEl.style.display = '';
    }
    else {
      drawingModeEl.innerHTML = 'Enter drawing mode';
      drawingOptionsEl.style.display = 'none';
    }
  };

  






  $('drawing-mode-selector').onchange = function() {

    if (this.value === 'hline') {
      canvas.freeDrawingBrush = vLinePatternBrush;
    }
    else if (this.value === 'vline') {
      canvas.freeDrawingBrush = hLinePatternBrush;
    }
    else if (this.value === 'square') {
      canvas.freeDrawingBrush = squarePatternBrush;
    }
    else if (this.value === 'diamond') {
      canvas.freeDrawingBrush = diamondPatternBrush;
    }
    else if (this.value === 'texture') {
      canvas.freeDrawingBrush = texturePatternBrush;
    }
    else {
      canvas.freeDrawingBrush = new fabric[this.value + 'Brush'](canvas);
    }

    if (canvas.freeDrawingBrush) {
      var brush = canvas.freeDrawingBrush;
      brush.color = drawingColorEl.value;
      if (brush.getPatternSrc) {
        brush.source = brush.getPatternSrc.call(brush);
      }
      brush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
      brush.shadow = new fabric.Shadow({
        blur: parseInt(drawingShadowWidth.value, 10) || 0,
        offsetX: 0,
        offsetY: 0,
        affectStroke: true,
        color: drawingShadowColorEl.value,
      });
    }
  };

  drawingColorEl.onchange = function() {
    var brush = canvas.freeDrawingBrush;
    brush.color = this.value;
    if (brush.getPatternSrc) {
      brush.source = brush.getPatternSrc.call(brush);
    }
  };
  drawingShadowColorEl.onchange = function() {
    canvas.freeDrawingBrush.shadow.color = this.value;
  };
  drawingLineWidthEl.onchange = function() {
    canvas.freeDrawingBrush.width = parseInt(this.value, 10) || 1;
    this.previousSibling.innerHTML = this.value;
  };
  drawingShadowWidth.onchange = function() {
    canvas.freeDrawingBrush.shadow.blur = parseInt(this.value, 10) || 0;
    this.previousSibling.innerHTML = this.value;
  };
  drawingShadowOffset.onchange = function() {
    canvas.freeDrawingBrush.shadow.offsetX = parseInt(this.value, 10) || 0;
    canvas.freeDrawingBrush.shadow.offsetY = parseInt(this.value, 10) || 0;
    this.previousSibling.innerHTML = this.value;
  };

  if (canvas.freeDrawingBrush) {
    canvas.freeDrawingBrush.color = drawingColorEl.value;
    canvas.freeDrawingBrush.source = canvas.freeDrawingBrush.getPatternSrc.call(this);
    canvas.freeDrawingBrush.width = parseInt(drawingLineWidthEl.value, 10) || 1;
    canvas.freeDrawingBrush.shadow = new fabric.Shadow({
      blur: parseInt(drawingShadowWidth.value, 10) || 0,
      offsetX: 0,
      offsetY: 0,
      affectStroke: true,
      color: drawingShadowColorEl.value,
    });
  }
})();
Comment

PREVIOUS NEXT
Code Example
Javascript :: Scale to fit 
Javascript :: reverse 
Javascript :: what happens if pass argument to a function that does not have parameters javascript 
Javascript :: erc721 abi json 
Javascript :: javascript online string concatenation 
Javascript :: LeagueFlysystemAwsS3v3AwsS3Adapter 
Javascript :: Constructing a URL from component parts and getting the constructed string 
Javascript :: slick slider on change 
Javascript :: toggleplay button javascript 
Javascript :: fetching coordinates from db to map 
Javascript :: js button that starts programe 
Javascript :: linux Error HH604: Error running JSON-RPC server 
Javascript :: starting: intent error type 3 react-native 
Javascript :: how to add header to axios request 
Javascript :: remove element 
Javascript :: symfony iterate over entity 
Javascript :: ArrayReplace 
Javascript :: creating a react app from scratch 
Javascript :: how to validate image binary in node js 
Javascript :: Timeout error when trying to use npx create-react-app 
Javascript :: searchable 
Javascript :: react-folder tree example 
Javascript :: mongodb-nodejs-driver, DeprecationWarning: collection.count is deprecated 
Javascript :: how to setup a webpack quickly 
Javascript :: Changing the value of a dropdown when the value of a number box changes in AngularJS 
Javascript :: angularjs How do I show all indicators for carousel in an ng-repeat 
Javascript :: How to query a button with specific text with react native testing library 
Javascript :: Extract and convert from JSON by Regex 
Javascript :: vue custom event validation 
Javascript :: aws amplify react site is blank after updating react-router-dom 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =