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

              
                <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>
              
            
!

CSS

              
                
              
            
!

JS

              
                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

Console