JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.

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

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

` ````
<div>
<label for="iterations">Select how many iterations should be computed (max of 25):</label>
<input type="number" id="iterations" name="iterations" max="25" min="1">
</div>
<canvas id="canvas" height="800" width="800"></canvas>
```

` ````
console.clear();
let iterations = 6;
const ctx = window.canvas.getContext('2d');
function line(startx, starty, endx, endy) {
ctx.beginPath();
ctx.moveTo(startx, starty);
ctx.lineTo(endx, endy);
ctx.stroke();
}
function drawLines(pointArray) {
ctx.beginPath();
ctx.moveTo(pointArray[0][0], pointArray[0][1]);
for (let i = 1; i < pointArray.length; i++) {
const nextPoint = pointArray[i];
ctx.lineTo(nextPoint[0],nextPoint[1]);
}
ctx.stroke();
}
//dragon curve utils
const cos45 = Math.cos(Math.PI / 4);
const sin45 = Math.sin(Math.PI / 4);
const cos135 = Math.cos(-Math.PI / 4);
const sin135 = Math.sin(-Math.PI / 4);
const sqrt2 = Math.sqrt(2);
const length = (p1, p2) => {
return Math.sqrt(Math.pow(p2[0] - p1[0],2) + Math.pow(p2[1] - p1[1],2));
}
let DRAGON_POINTS_SEQUENCE = [];
function dragon(points, order = 1) {
if (order === 0) {
drawLines(points);
} else {
let newPointsArray = [];
for (let i = 0; i < points.length - 1; i++) {
if (i % 2 === 0 ) {
// console.log("even: ", [points[i], points[i + 1]]);
// console.log(f1(points[i], points[i + 1]))
newPointsArray.push(f1(points[i], points[i + 1]));
} else if (i % 2 === 1) {
// console.log("odd: ", [points[i], points[i + 1]]);
// console.log(f2(points[i], points[i + 1]))
newPointsArray.push(f2(points[i], points[i + 1]));
}
}
let newPoints = []
// console.log("points: ", points);
// console.log("new points: ", newPointsArray);
for (let i = 0; i < newPointsArray.length; i++) {
newPoints.push(points[i])
newPoints.push(newPointsArray[i]);
}
newPoints.push(points[points.length-1]);
// console.log("result", newPoints);
order--;
DRAGON_POINTS_SEQUENCE.push(newPoints);
dragon(newPoints, order);
}
}
function f1(p1, p2) {
const refVector = [
(p2[0]-p1[0]) / sqrt2,
(p2[1]-p1[1]) / sqrt2,
];
let newV = [
refVector[0] * cos45 - refVector[1] * sin45,
refVector[0] * sin45 + refVector[1] * cos45
];
newV = [ newV[0]+ p1[0], newV[1] + p1[1] ];
return newV;
}
function f2(p1, p2) {
const refVector = [
(p2[0]-p1[0]) / sqrt2,
(p2[1]-p1[1]) / sqrt2,
];
let newV = [
refVector[0] * cos45 - refVector[1] * -sin45,
refVector[0] * -sin45 + refVector[1] * cos45
];
newV = [ newV[0] + p1[0], newV[1] + p1[1] ];
return newV;
}
// Engage!
let points = [[250,250],[700,250]];
dragon(points, 3);
window.iterations.addEventListener('change', (event) => {
ctx.clearRect(0, 0, window.canvas.width, window.canvas.height);
DRAGON_POINTS_SEQUENCE = [];
dragon(points, event.target.value);
console.log(DRAGON_POINTS_SEQUENCE);
});
// Dragon points sequence is to collect the points of each iteration up to the selected iteration
console.log(DRAGON_POINTS_SEQUENCE);
```

999px

Shift F
Opt F
Find & Replace

Also see: Tab Triggers