Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <header>
  <h1>SVG Brush (first test)</h1>
  <h2>Drawing SVG paths on Canvas with Photoshop-like brushes</h2>
  <h3><a href="https://codepen.io/lbebber">By Lucas Bebber</a></h3>
</header>
<canvas width="400" height="250"></canvas> 
<br />
<button class="button-rough">Rough Brush</button>
<button class="button-oval">Oval Brush</button>

<p>(from this SVG)</p>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
   width="390px" height="249px" viewBox="0 0 390 249" enable-background="new 0 0 390 249" xml:space="preserve">
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M28.089,48.172c2.339,49.633-7.635,102.116-5.925,151.969
  c26.925-4.92,54.728-15.152,81.318-23.453"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M114.426,104.849c-2.639,18.624-2.253,44.778,14.806,52.441
  c19.732,8.863,34.095-12.15,37.325-32.19c2.042-12.674-0.671-25.136-3.49-36.345"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M222.652,78.647c-50.478-0.752-61.612,94.508,3.648,55.094"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M238.46,135.798c-0.364-22.483,14.526-52.199,25.203-74.705
  c15.474,16.676,16.932,44.386,29.519,63.288"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M338.176,47.112c-20.311-0.576-63.37,24.898-29.595,35.328
  c8.631,2.666,79.448-10.634,38.467,26.769c-13.665,12.47-32.776,8.171-44.138,1.982"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M226.3,108.205c19.693-2.995,44.241-11.857,64.449-18.527"/>
<path fill="none" stroke="#231F20" stroke-miterlimit="10" d="M96,226c33.96-29.328,85.695-38.628,127.121-51.798
  C256.447,163.608,289.277,151.283,323,142"/>
</svg>
              
            
!

CSS

              
                
body
  background #fafafa
  text-align center
  padding-left 30px
  padding-right @padding-left
  color #334
  font-family Baskerville, Georgia, serif

a
  color inherit
  &:visited
    color #555
    
header
  padding-top 20px
  
h1,h2,h3
  margin 0
  margin-bottom 20px
  font-weight normal
  
h1
  font-size 3em
  
h3
  font-size 1.2em
  
button
  background transparent
  border 1px solid #ccc
              
            
!

JS

              
                var Brush;
var BrushTip;
var Tips={};
(function() {
  "use strict";

  function checkObject(obj) {
    return typeof obj != "object" ? {} : obj;
  }

  function checkNumber(number, defaultValue) {
    return typeof number != "number" ? defaultValue : number;
  }
  
  Brush = function(options) {
    options = checkObject(options);
    this.path = options.path || null;
    this.ctx = options.ctx || null;
    this.spacing = checkNumber(options.spacing, 0);
    this.tip = options.tip || new Tips.Rough1();

    if (this.path != null)
      this.pathLength = this.path.getTotalLength();
  }

  Brush.prototype = {
    path: null,
    pathLength: 0,
    v: 0,
    lastV: 0,
    ctx: null,
    spacing: 0,
    tip: null,
    getPos: function(v) {
      v = this._cleanV(v);

      return this.path.
      getPointAtLength(
        v * this.pathLength
      );
    },
    draw: function() {
      var v = this._getV();

      this._render(v);

      this.lastV = v;
    },
    drawFromLast: function() {
      var v = this._getV();

      var lastV = this.lastV;
      var spacing = this.spacing;

      if (spacing > 0) {
        var dist = Math.abs(v - lastV);
        var step = this._lenToV(spacing);
        var steps = Math.ceil(dist / step);
        step = dist / steps;

        var curV = lastV;
        var timeout=1000;
        while (timeout>0 && ((v > lastV && curV <= v) || (v < lastV && curV >= v))) {
          timeout--;
          this._render(curV, {
            rotation: 0,
            pressure: Math.sqrt(1 - (Math.abs(v - 0.5) * 2))
          });
          curV += step;
        }
      }

      this.lastV = v;
    },
    _vToLen: function(v) {
      return v * this.pathLength;
    },
    _lenToV: function(len) {
      return len / this.pathLength;
    },
    _getV: function() {
      return this._cleanV(this.v);
    },
    _cleanV: function(v) {
      v = checkNumber(v, this.v);
      if (v > 1) v = 1;
      if (v < 0) v = 0;
      return v;
    },
    _render: function(v, options) {
      v = this._cleanV(v);

      options = checkObject(options);

      var rotation = checkNumber(options.rotation,0);
      var pressure=checkNumber(options.pressure,1);

      var ctx = this.ctx;
      var pos = this.getPos(v);
      
      ctx.save();
      
      ctx.translate(pos.x, pos.y);

      this.tip.draw(ctx, {
        pressure: pressure
      });

      ctx.restore();
      
    }
  }

  BrushTip = function(options) {
    options = checkObject(options);
    this.size=checkNumber(options.size,1);
  };
  BrushTip.prototype = {
    size:1,
    _currentCtx: null,
    _currentOptions:{},
    draw: function(ctx, options) {
      options = checkObject(options);
      this._currentCtx = ctx;
      this._render(options);
    },
    _render:function(options){
      
    },
    _drawCircle: function(r, x, y, polar) {
      x = checkNumber(x, 0);
      y = checkNumber(y, 0);
      if (polar) {
        var a = x;
        var d = y;
        x = Math.cos(a) * d;
        y = Math.sin(a) * d;
      }
      var ctx = this._currentCtx;

      ctx.beginPath();
      ctx.arc(x, y, r, 0, Math.PI * 2);
      ctx.fill();
      ctx.closePath()
    }
  }
  
  Tips.Round=function(options){
    BrushTip.call(this,options);
  }
  Tips.Round.prototype=Object.create(BrushTip.prototype);
  Tips.Round.prototype.constructor=Tips.Round;
  Tips.Round.prototype._render=function(options){
    BrushTip.prototype._render.call(this,options);
    var pressure = checkNumber(options.pressure, 1);
    var ctx=this._currentCtx;
    
    ctx.scale(1,0.4);
    this._drawCircle(6*this.size);
  }
  
  
  // Rough brush
  Tips.Rough1=function(options){
    BrushTip.call(this,options);
    
    var maxDist=8;
    this._points=new Array();
    for (var i = 0; i < 15; i++) {
      var d=Math.pow(Math.random(),2);
      var a=Math.random()*Math.PI*2;
      var r=0.1+(Math.pow(1-d,2)*4);
      d*=maxDist;
      this._points.push({
        d:d,
        a:a,
        r:r
      });
    } 
    console.log(this);
  }
  Tips.Rough1.prototype=Object.create(BrushTip.prototype);
  Tips.Rough1.prototype.constructor=Tips.Rough1;
  Tips.Rough1.prototype._points=null;
  Tips.Rough1.prototype._render=function(options){
    BrushTip.prototype._render.call(this,options);
 
    var pressure = checkNumber(options.pressure, 1);
    var size=this.size;
    var ctx=this._currentCtx;

    ctx.fillStyle = "rgba(0,0,0,0.8)";

    var that=this;
    this._points.forEach(function(point,i){
      that._drawCircle(
        point.r*pressure,
        point.a,
        point.d,
        true
      );
    })
  }
}());

document.addEventListener('DOMContentLoaded', function() {
  "use strict";

  var ctx = document.
  querySelector("canvas").
  getContext("2d"),
    svg = document.
  querySelector("svg"),
    paths = svg.
  querySelectorAll("path"),

  roughBrushes = [],
  ovalBrushes = [];
  
  for (var i = 0; i < paths.length; i++) {
    var path = paths[i];
    var roughBrush = new Brush({
      path: path,
      ctx: ctx,
      spacing: 1
    });
    
    roughBrushes.push(roughBrush);
    
    
    var ovalBrush = new Brush({
      path: path,
      ctx: ctx,
      spacing: 2,
      tip:new Tips.Round()
    });
    
    ovalBrushes.push(ovalBrush);
  }
  
  function animateBrush(brush,i){
    var updateBrush=function(){
      brush.drawFromLast();
    }
    TweenMax.to(brush,1,{
      v:1,
      delay:0.15*i,
      ease:Quint.easeOut,
      onUpdate:updateBrush,
      onComplete:updateBrush
    })
  }
  
  function clear(){
    var resetBrush=function(brush){
      TweenMax.killTweensOf(brush);
      brush.v=0;
      brush.draw();
    }
    roughBrushes.forEach(resetBrush);
    ovalBrushes.forEach(resetBrush);
    ctx.clearRect(0,0,400,250);
  }
  
  function animateRough(){
    clear();
    roughBrushes.forEach(animateBrush);
  }
  function animateOval(){
    clear();
    ovalBrushes.forEach(animateBrush);
  }
  animateRough();
  
  document.querySelector('.button-rough').addEventListener('click',function(){
    animateRough();
  });
  document.querySelector('.button-oval').addEventListener('click',function(){
    animateOval();
  })
}, false);
              
            
!
999px

Console