Skip to content

Commit

Permalink
Added some constants to fractal.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dkreft committed Nov 13, 2022
1 parent 9c57382 commit 8f4093d
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/components/App/Fractal/lib/fractal.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,30 @@ import getDirection from './getDirection'

const D2R = Math.PI / 180

const CANVAS_BACKGROUND_COLOR = '#fff'
const MIN_SIDE_LENGTH = 1.5
const SHAPE_FILL_COLOR = 'rgba(0, 0, 0, 0.15)'
const STROKE_COLOR = '#000'


export default function drawFractal(canvas, opts = {}) {
const ratio = Math.min(0.999, opts.ratio)

const ctx = canvas.getContext('2d')
ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, canvas.width, canvas.height);
const ctx = makeContext(canvas)

ctx.translate(canvas.width * 0.5,
canvas.height * 0.5)

const root = buildShape({
direction: opts.direction,
length: opts.length,
startX: -(opts.length / 2),
startY: (opts.length / 3),
})

const shapesToDraw = [root]

const lengths = new Set()

const shapesToDraw = [
buildShape({
length: opts.length,
startX: -(opts.length / 2),
startY: (opts.length / 3),
})
]

while ( shapesToDraw.length ) {
const shape = shapesToDraw.shift()

Expand Down Expand Up @@ -68,12 +67,21 @@ export default function drawFractal(canvas, opts = {}) {
}
}

function makeContext(canvas) {
const ctx = canvas.getContext('2d')

ctx.fillStyle = CANVAS_BACKGROUND_COLOR
ctx.fillRect(0, 0, canvas.width, canvas.height);

return ctx
}

function drawPolygon(ctx, { direction, length, points }, strokeColor) {
const [start, ...others] = points

ctx.beginPath()
ctx.strokeStyle = strokeColor
ctx.fillStyle = `rgba(0, 0, 0, 0.15)`
ctx.fillStyle = SHAPE_FILL_COLOR
ctx.moveTo(start.x, start.y)

others.forEach(({ x, y }, i) => {
Expand All @@ -86,7 +94,7 @@ function drawPolygon(ctx, { direction, length, points }, strokeColor) {
ctx.stroke()
}

function buildShape({ direction, length, startX, startY }) {
function buildShape({ direction = 0, length, startX, startY }) {
const points = [
{
x: startX,
Expand Down

0 comments on commit 8f4093d

Please sign in to comment.