Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <canvas id="space"></canvas>
<div id="info">
  <h1>StringArt avec FESC</h1>
  <p>
    Devenez vous aussi un artiste numérique ;)<br>
    Rr: <span id="r"></span> G: <span id="g"></span> B: <span id="b"></span> x: <span id="x"></span> y: <span id="y"></span><br>
    Cliquez sur l'image pour générer votre oeuvre d'art numérique interactive !
  </p>
</div>
              
            
!

CSS

              
                html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

body{
  position: relative;
  background-color: #333333;
}

#info {
  position: absolute;
  top: 0px;
  left: 0px;
  margin: 12px 0 0 12px;
}
h1 {
  color: #eeeeee;
  font-size: 21px;
  font-weight: normal;
  margin: 0;
}
p, a {
  margin: 3px 0 0 0;
  color: #cccccc;
  font-size: 13px;
  line-height: 20px;
}
p span {
  display: inline-block;
  width: 25px;
  color: #ffffff;
}
              
            
!

JS

              
                var main = (function () {
	
	var canvas, ctx;
	var deg360 = Math.PI*2;
  var r, g, b;
  var dom = {};
  var count = 0;
  var last = 5;
	var latency = 5;
  var points = [];
  
	var randomRange = function (min, max) {
		return min + Math.random() * (max - min);
	};
    
	var randomRangeInt = function (min, max) {
		return Math.round(randomRange(min, max));
	}
	
	var cropInt = function (min, max, int) {
		return Math.min(max, Math.max(min, int));
	}
	
	var colorCompChange = function (colorComp) {
		return cropInt(50, 200, colorComp + randomRangeInt(-15, 15));
	}
  
  var circle = function (x, y, r, c) {
    ctx.beginPath();
    ctx.moveTo(x,y);
    ctx.arc(x, y, r, 0, deg360, false);
    ctx.fillStyle = 'rgb(' + c.join(',') + ')';
    ctx.fill();
  };
  
  var line = function (sx, sy, ex, ey, w, c) {
    ctx.beginPath();
    ctx.moveTo(sx, sy);
    ctx.lineTo(ex, ey);
    ctx.lineWidth = 1;
    ctx.strokeStyle = 'rgba(' + c.join(',') + ', 0.5)';
    ctx.stroke();
  };
  
  var drawNextPoint = function (x, y) {
    // update color components
    r = colorCompChange(r);
    g = colorCompChange(g);
    b = colorCompChange(b);
    
    // display corrdinates and color components
    var v = {x:x, y:y, r:r, g:g, b:b};
    for (var k in v)
      dom[k].innerHTML = v[k];
    
    // draw current point
    circle(x, y, 3, [r, g, b]);
    
		// connect to last 5 points
		for (var i = 0; i < points.length; i++) {
      var w = randomRange(0.2, 3);
      line(x, y, points[i].x, points[i].y, w, [r, g, b]);
		}
		
		// (re)set the last 5 points
    if (points.length > last) {
			points.splice(0, 1);
    }
		points.push( { x: x, y: y } );
  };
  
  var startOver = function () {
    points = [];
    ctx.clearRect (0, 0, canvas.width, canvas.height);
  };
  
  var demoCount = 0;
  var demo = function () {
    demoCount++;
    var x, y;
    if (points.length==0) {
      x = 240;
      y = 80;
    } else {
      x = points[points.length-1].x;
      y = points[points.length-1].y;
    }
		x = cropInt(0, canvas.width, x + randomRangeInt(-35, 35));
		y = cropInt(0, canvas.height, y + randomRangeInt(-35, 35));
    drawNextPoint(x, y);
    if (demoCount < 100) {
      setTimeout(arguments.callee, 1000/60);
    }
  };
  
  var start = function () {
    canvas = document.getElementById('space');
    canvas.width  = window.innerWidth;
    canvas.height = window.innerHeight;
    ctx = canvas.getContext('2d');
    
    var spans = 'rgbxy';
    for (var i=0; i<spans.length; i++)
      dom[spans[i]] = document.getElementById(spans[i]);
    
		r = randomRangeInt(50, 200);
		g = randomRangeInt(50, 200);
		b = randomRangeInt(50, 200);
    
    canvas.addEventListener('click', startOver, false);
    canvas.addEventListener('mousemove', function (e) {
			count++;
      if (count % latency == 0) {
				drawNextPoint(e.clientX, e.clientY);
      }
    }, false);
    
    demo();
  };
  
  // domReady
  if (document.addEventListener) {
    document.addEventListener('DOMContentLoaded', function () {
      document.removeEventListener('DOMContentLoaded', arguments.callee, false);
      setTimeout(start, 60);
    }, false );
  }
  
})();

              
            
!
999px

Console