/**
* Ken Fyrstenberg Nilsen
* Abidas Software
*/
var canvas = document.getElementById('canvas'),
ctx = canvas.getContext('2d');
document.getElementById('new').addEventListener('click', doCanvas, false);
/**
* Functions for demo
*/
function doCanvas() {
ctx.clearRect(0,0,canvas.width, canvas.height * 0.7);
var numOfSegments = 9;
var segment = canvas.width / numOfSegments;
var i;
var points = [];
var variations = 0.22;
//produce some random heights across the canvas
for(i=0; i < numOfSegments + 1; i++) {
points.push(segment * i);
points.push(canvas.height / 2.8 + canvas.height * variations * Math.random());
}
ctx.beginPath();
ctx.moveTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
ctx.curve(points); //modfied curve overrides tension
ctx.closePath();
ctx.fillStyle = 'green';
ctx.fill();
}
/**
* Init generals
*/
function download() {
var dt = canvas.toDataURL();
this.href = dt; //this may not work in the future..
}
document.getElementById('download').addEventListener('click', download, false);
/*!
* curve (cardinal spline) extension for canvas version 1.0
*
* This is the optimized version of drawCurve()
*
* By Ken Fyrstenberg / Abdias Software (abdiassoftware.com) 2013
* Source code may be used for any kind of project.
*/
CanvasRenderingContext2D.prototype.curve = function(pts, tension, numOfSegments) {
// use input value if provided, or use a default value
tension = (tension != 'undefined') ? tension : 0.5;
numOfSegments = numOfSegments ? numOfSegments : 16;
var _pts = [], res = [], // clone array
x, y, // our x,y coords
t1x, t2x, t1y, t2y, // tension vectors
c1, c2, c3, c4, // cardinal points
st, st2, st3, st23, st32, // steps
t, i, l, r = 0;
// clone array so we don't change the original
_pts = pts.concat();
_pts.unshift(pts[1]); //copy 1. point and insert at...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.