SVG Draw

In this example below you will see how to do a SVG Draw with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by tmanderson, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright tmanderson ©

Technologies

  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>SVG Draw</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>

</head>

<body>

  <div id="drawing"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/svg.js/2.3.0/svg.min.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/tmanderson/svg-draw-Yqmzbd */
// create svg drawing
var draw = SVG('drawing')
var line;
var points;

draw.node.onmousedown = function(e) {
  points = [[e.clientX, e.clientY]]
  line = draw.polyline(points).fill('none');
}

draw.node.onmousemove = function(e) {
  if(!line) return;
  points.push([e.clientX,e.clientY]);
  line.plot(points).stroke({ width: 2 });
}

draw.node.onmouseup = function(e) {
  line.stroke({ width: 1 });
  line = null;
}

// create image
var image = draw.image('http://www.planwallpaper.com/static/images/colorful-triangles-background_yB0qTG6.jpg')
image.size(650, 800).y(-325);

// create text
var text = draw.text('SVG.JS').move(325, 0)

text.font({
  family: 'Source Sans Pro'
  , size: 125
  , anchor: 'middle'
  , leading: 1
})

// clip image with text
image.clipWith(text)

var two = SVG(document.querySelector('svg'))

Comments