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='drawing' width = 600 height=600>
</canvas> 
              
            
!

CSS

              
                #drawing{
  border: 1px solid black
}
              
            
!

JS

              
                function rad(degrees)
{
  return (Math.PI / 180) * degrees
}

let canvas = document.getElementById('drawing')
let ctx = canvas.getContext('2d')

let t = 1;
function pixel(x, y, color)
{
  ctx.fillRect(x, y, 1, 1)
}

function elIn(el, iter)
{
  for (let i in iter) if (iter[i] == el) return true
  return false
}
function inRule(el, iter)
{
  for (let i in iter) if (i == el) return true
  return false
}

function lg (iter, base, rule)
{
  if (iter == 0 ) return base
  let s = ''
  let cur = base;
  for (let k = 0; k < iter; k++)
    { s = ''
      for (let i in cur) 
        {
          if (inRule(cur[i], rule))
            {
              s += rule[cur[i]]
            }
          else s += cur[i]
        }
      cur = s;
    }
  return cur;
}
let angle = 0
let x = 300, y =300
let base_angle = 90
let distance = 50
function forward(distance)
{ ctx.beginPath()
  ctx.moveTo(x, y)
  let x_destination = distance
  let y_destination = 0;
  let x_rotated = x_destination * Math.cos(rad(angle)) - y_destination * Math.sin(rad(angle))
  let y_rotated = x_destination  * Math.sin(rad(angle)) + y_destination * Math.cos(rad(angle)) 
  x_rotated += x
  y_rotated += y
  ctx.strokeStyle = distColor(x_rotated, y_rotated)
  ctx.lineTo(x_rotated, y_rotated)
  ctx.stroke()
 ctx.closePath()
  x = x_rotated
  y = y_rotated
  x_max = Math.max(x_max, x)
  x_min = Math.min(x_min, x)
  y_max = Math.max(y_max, y)
  y_min = Math.min(y_min, y)
  
}

function right()
{
 angle += base_angle 
}
function left()
{
  angle -= base_angle
}
function draw_lg (s_lg)
{ ctx.lineWidth=3
  ctx.beginPath()
  ctx.moveTo(x, y)
  for (let i of s_lg)
    {
      if (i == 'F') forward(distance)
      if (i == '+') left()
      if (i == '-') right()
    }
   ctx.stroke()
   ctx.closePath()
   angle = 0
}

x_max = 0
x_min = 1000
y_max = 0
y_min = 1000

function distColor(x, y)
{ 
  let r = (x - 300) * (x - 300) + (y -300) *(y-300)
  if (x > 300)
  return `rgb(${100},50,${200 * Math.sqrt(r)/300})`
  else 
     return `rgb(${200 *Math.sqrt(r)/300},50,${100})`
     //`rgb(${200 * Math.sqrt(r)/300},100,${200 * Math.sqrt(r)/300})`
}
 let x_start = 100
 let y_start = 450
function draw(iter)
{ 
  x_max = 0
  x_min = 1000
  y_max = 0
  y_min = 1000
  let  s_lg = lg(iter, axiom, rules)
  //distance = 20 * 580.6888370749682/length[iter] 
  distance =  358.8854381999805 / Math.pow(coeff, iter)
  x = x_start
  y = y_start
  //draw_lg(s_lg)
  ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
  ctx.fillRect(0, 0, canvas.width,     canvas.height);
  //console.log(distance)
  //center_x = 300 - (x_max + x_min)/2
  //center_y = 300 - (y_max + y_min)/2
  // 20.55728090000997 126.13395122000176
 //console.log(center_x, center_y)
  x = x_start+20.55728090000997
  y = y_start+126.13395122000176
  draw_lg(s_lg)
  angle = 0
}
let coeff = 2.618033988749895;
distance = 358.8854381999805;
//length =[32.36067977499789,84.72135954999578,221.80339887498943,580.6888370749682]
//for (let i = 0;i<3;i++) console.log(length[i+1]/length[i])
axiom = "F++F++F++F++F"
rules = {"F":"F++F++F+++++F-F++F"}
iterations = 3 
base_angle = 36
distance = 20

/*s = lg(3, axiom, rules)
console.log(s)
x_start = 100
y_start = 450
x = x_start
y = y_start
draw_lg(s)
ctx.clearRect(0,0,600,600)

center_x = 300 - (x_max + x_min)/2
center_y = 300 - (y_max + y_min)/2


x = x_start+center_x
y = y_start+center_y
draw_lg(s)
*/
/*ctx.clearRect(0,0, 600,600)
ctx.translate(-center_x, -center_y)
x = x_start
y = y_start
draw_lg(s)
*/

//draw(5)

iter = 0
let timerId = setInterval(() => {draw(iter); iter+=1; if (iter>5) iter =0;}, 3000);

              
            
!
999px

Console