Code Sketch


yadesh shahare
By: Mhalsakant School
cleari()
originBottomLeft()

// ============================================================
// GOLDEN BUDDHA - WISDOM AND PEACE
// CLEAN KOJO SCALA VERSION
// ============================================================

// ------------------------------------------------------------
// ANIMATION VARIABLES
// ------------------------------------------------------------

var t = 0.0
var rayT = 0.0
var glowT = 0.0
var waterT = 0.0
var lotusT = 0.0
var sparklePhase = 0.0

var meditation = false

// ------------------------------------------------------------
// STAR FIELD
// ------------------------------------------------------------

val starCount = 180

val starX =
  Array.fill(starCount)(
    10.0 + math.random * (cwidth - 20.0)
  )

val starY =
  Array.fill(starCount)(
    170.0 + math.random * (cheight - 190.0)
  )

val starSize =
  Array.fill(starCount)(
    1.0 + math.random * 2.5
  )

// ------------------------------------------------------------
// GOLDEN PARTICLES
// ------------------------------------------------------------

case class Particle(
  var angle: Double,
  var radius: Double,
  var speed: Double,
  var size: Double
)

val particles =
  scala.collection.mutable.ArrayBuffer.empty[Particle]

for (i <- 0 until 90) {

  particles.append(
    Particle(
      math.random * math.Pi * 2.0,
      120.0 + math.random * 210.0,
      0.002 + math.random * 0.004,
      1.5 + math.random * 3.5
    )
  )
}

// ------------------------------------------------------------
// KNOWLEDGE WORD PARTICLES
// ------------------------------------------------------------

case class WordParticle(
  var x: Double,
  var y: Double,
  var speed: Double,
  var phase: Double,
  var index: Int
)

val words =
  Array(
    "WISDOM",
    "PEACE",
    "KNOWLEDGE",
    "COMPASSION"
  )

val wordParticles =
  scala.collection.mutable.ArrayBuffer.empty[WordParticle]

for (i <- 0 until 16) {

  wordParticles.append(
    WordParticle(
      40.0 + math.random * (cwidth - 80.0),
      155.0 + math.random * 270.0,
      0.12 + math.random * 0.30,
      math.random * 6.28,
      i % words.length
    )
  )
}

// ============================================================
// UPDATE PARTICLES
// ============================================================

def updateParticles(): Unit = {

  particles.foreach { p =>
    p.angle += p.speed
  }

  wordParticles.foreach { p =>

    p.y += p.speed

    p.x +=
      math.sin(
        t + p.phase
      ) * 0.25

    if (p.y > cheight - 50) {

      p.y = 145.0

      p.x =
        40.0 +
        math.random * (cwidth - 80.0)
    }
  }
}

// ============================================================
// SKY
// ============================================================

def drawSky(
  c: CanvasDraw
): Unit = {

  c.background(
    8,
    12,
    20
  )

  c.fill(
    70,
    42,
    24,
    80
  )

  c.noStroke()

  c.rect(
    0,
    150,
    cwidth,
    cheight - 150
  )

  for (i <- 0 until starCount) {

    val alpha =
      (
        80 +
        math.abs(
          math.sin(
            t * 2.0 + i
          )
        ) * 170
      ).toInt

    c.fill(
      255,
      225,
      145,
      alpha
    )

    c.ellipse(
      starX(i),
      starY(i),
      starSize(i),
      starSize(i)
    )
  }

  for (i <- 0 until 20) {

    val x =
      30.0 +
      i * 37.0

    val y =
      210.0 +
      math.sin(
        t + i
      ) * 80.0

    c.stroke(
      255,
      225,
      150,
      120
    )

    c.strokeWeight(1)

    c.line(
      x - 4,
      y,
      x + 4,
      y
    )

    c.line(
      x,
      y - 4,
      x,
      y + 4
    )
  }
}

// ============================================================
// MOUNTAINS
// ============================================================

def drawMountains(
  c: CanvasDraw
): Unit = {

  c.fill(
    48,
    68,
    74
  )

  c.noStroke()

  c.beginShape()

  c.vertex(0, 125)
  c.vertex(80, 225)
  c.vertex(155, 155)
  c.vertex(245, 235)
  c.vertex(340, 165)
  c.vertex(450, 240)
  c.vertex(550, 155)
  c.vertex(cwidth, 225)
  c.vertex(cwidth, 125)

  c.endShape()

  c.fill(
    85,
    110,
    115,
    100
  )

  c.beginShape()

  c.vertex(0, 140)
  c.vertex(80, 210)
  c.vertex(155, 175)
  c.vertex(245, 220)
  c.vertex(340, 180)
  c.vertex(450, 225)
  c.vertex(550, 170)
  c.vertex(cwidth, 215)
  c.vertex(cwidth, 140)

  c.endShape()
}

// ============================================================
// BODHI TREE
// THIS FUNCTION FIXES "drawTree NOT FOUND"
// ============================================================

def drawTree(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  // Main trunk
  c.fill(
    52,
    31,
    22
  )

  c.stroke(
    30,
    18,
    14
  )

  c.strokeWeight(3)

  c.rect(
    cx - 105,
    105,
    210,
    310
  )

  // Roots and branches
  c.stroke(
    48,
    28,
    20
  )

  c.strokeWeight(11)

  c.line(
    cx - 50,
    310,
    cx - 190,
    430
  )

  c.line(
    cx - 35,
    350,
    cx - 120,
    470
  )

  c.line(
    cx + 50,
    310,
    cx + 190,
    430
  )

  c.line(
    cx + 35,
    350,
    cx + 120,
    470
  )

  // Trunk highlights
  c.stroke(
    100,
    65,
    40,
    160
  )

  c.strokeWeight(4)

  c.line(
    cx - 60,
    125,
    cx - 28,
    360
  )

  c.line(
    cx - 10,
    125,
    cx + 2,
    350
  )

  c.line(
    cx + 55,
    125,
    cx + 32,
    350
  )

  // Leaves / canopy
  for (i <- 0 until 34) {

    val angle =
      i * 11.0 +
      t * 2.0

    val rad =
      math.toRadians(angle)

    val radius =
      120.0 +
      (i % 5) * 24.0

    val x =
      cx +
      math.cos(rad) * radius

    val y =
      395.0 +
      math.sin(rad) * 80.0 +
      math.sin(t + i) * 4.0

    if (i % 3 == 0) {

      c.fill(
        38,
        100,
        48
      )

    } else {

      c.fill(
        48,
        125,
        55
      )
    }

    c.noStroke()

    c.ellipse(
      x,
      y,
      75,
      50
    )
  }
}

// ============================================================
// WATER
// ============================================================

def drawWater(
  c: CanvasDraw
): Unit = {

  c.fill(
    15,
    35,
    40
  )

  c.noStroke()

  c.rect(
    0,
    0,
    cwidth,
    95
  )

  for (i <- 0 until 22) {

    val y =
      7.0 +
      i * 4.0

    val w =
      80.0 +
      i * 25.0 +
      math.abs(
        math.sin(
          waterT + i * 0.45
        )
      ) * 30.0

    c.stroke(
      150,
      190,
      175,
      55
    )

    c.strokeWeight(1)

    c.line(
      cwidth / 2.0 - w / 2.0,
      y,
      cwidth / 2.0 + w / 2.0,
      y
    )
  }
}

// ============================================================
// SUN
// ============================================================

def drawSun(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  val cy =
    385.0

  val pulse =
    1.0 +
    math.sin(glowT) * 0.035

  c.fill(
    255,
    170,
    30,
    18
  )

  c.noStroke()

  c.ellipse(
    cx,
    cy,
    420.0 * pulse,
    420.0 * pulse
  )

  c.fill(
    255,
    190,
    45,
    28
  )

  c.ellipse(
    cx,
    cy,
    350.0 * pulse,
    350.0 * pulse
  )

  c.fill(
    255,
    220,
    90,
    55
  )

  c.ellipse(
    cx,
    cy,
    280.0 * pulse,
    280.0 * pulse
  )

  c.fill(
    255,
    198,
    55
  )

  c.stroke(
    255,
    235,
    150
  )

  c.strokeWeight(4)

  c.ellipse(
    cx,
    cy,
    215.0 * pulse,
    215.0 * pulse
  )

  for (i <- 0 until 60) {

    val ang =
      i * 31.0

    val rad =
      math.toRadians(ang)

    val r =
      35.0 +
      (i % 6) * 15.0

    val px =
      cx +
      math.cos(rad) * r

    val py =
      cy +
      math.sin(rad) * r

    c.fill(
      255,
      230,
      130,
      130
    )

    c.noStroke()

    c.ellipse(
      px,
      py,
      4.0 + (i % 4),
      4.0 + (i % 4)
    )
  }
}

// ============================================================
// KNOWLEDGE RAYS
// ============================================================

def drawKnowledgeRays(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  val cy =
    385.0

  for (i <- 0 until 56) {

    val angle =
      rayT +
      i * 360.0 / 56.0

    val rad =
      math.toRadians(angle)

    val inner =
      110.0

    val outer =
      170.0 +
      math.abs(
        math.sin(
          t * 2.0 + i
        )
      ) * 70.0

    val x1 =
      cx +
      math.cos(rad) * inner

    val y1 =
      cy +
      math.sin(rad) * inner

    val x2 =
      cx +
      math.cos(rad) * outer

    val y2 =
      cy +
      math.sin(rad) * outer

    if (i % 4 == 0) {

      c.stroke(
        255,
        240,
        180,
        190
      )

    } else {

      c.stroke(
        255,
        195,
        60,
        105
      )
    }

    c.strokeWeight(
      1.0 +
      math.abs(
        math.sin(
          rayT + i
        )
      ) * 1.5
    )

    c.line(
      x1,
      y1,
      x2,
      y2
    )
  }
}

// ============================================================
// HALO
// ============================================================

def drawHalo(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  val cy =
    300.0

  val breathe =
    1.0 +
    math.sin(
      t * 1.5
    ) * 0.03

  c.noFill()

  c.stroke(
    255,
    235,
    175,
    150
  )

  c.strokeWeight(4)

  c.ellipse(
    cx,
    cy,
    205.0 * breathe,
    205.0 * breathe
  )

  c.stroke(
    255,
    190,
    65,
    75
  )

  c.strokeWeight(7)

  c.ellipse(
    cx,
    cy,
    240.0 * breathe,
    240.0 * breathe
  )
}

// ============================================================
// BUDDHA
// ============================================================

def drawBuddha(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  val bob =
    math.sin(
      t * 1.4
    ) * 2.0

  val faceY =
    300.0 + bob

  // Robe
  c.fill(
    180,
    105,
    54
  )

  c.stroke(
    105,
    60,
    34
  )

  c.strokeWeight(2)

  c.ellipse(
    cx,
    155,
    245,
    150
  )

  c.fill(
    195,
    120,
    66
  )

  c.ellipse(
    cx,
    195,
    155,
    190
  )

  // robe folds
  c.stroke(
    110,
    62,
    35,
    170
  )

  c.strokeWeight(2)

  for (i <- 0 until 11) {

    val yy =
      105.0 +
      i * 12.0

    c.arc(
      cx,
      yy,
      180.0 - i * 5.0,
      30.0,
      0,
      math.Pi
    )
  }

  // Neck
  c.fill(
    194,
    132,
    95
  )

  c.noStroke()

  c.rect(
    cx - 18,
    235,
    36,
    48
  )

  // Face
  c.fill(
    207,
    148,
    108
  )

  c.stroke(
    125,
    77,
    55
  )

  c.strokeWeight(2)

  c.ellipse(
    cx,
    faceY,
    77,
    91
  )

  // Ears
  c.fill(
    190,
    127,
    91
  )

  c.ellipse(
    cx - 42,
    faceY,
    17,
    37
  )

  c.ellipse(
    cx + 42,
    faceY,
    17,
    37
  )

  // Hair
  c.fill(
    45,
    29,
    22
  )

  c.noStroke()

  c.ellipse(
    cx,
    faceY + 37,
    54,
    40
  )

  for (i <- 0 until 20) {

    val ang =
      i * 18.0

    val rad =
      math.toRadians(ang)

    c.ellipse(
      cx +
        math.cos(rad) * 18,
      faceY +
        38 +
        math.sin(rad) * 12,
      12,
      12
    )
  }

  // Eyes
  c.noFill()

  c.stroke(
    75,
    43,
    36
  )

  c.strokeWeight(2.4)

  c.arc(
    cx - 16,
    faceY + 4,
    24,
    13,
    math.Pi,
    math.Pi * 2
  )

  c.arc(
    cx + 16,
    faceY + 4,
    24,
    13,
    math.Pi,
    math.Pi * 2
  )

  // Nose
  c.line(
    cx,
    faceY - 3,
    cx - 3,
    faceY - 19
  )

  // Smile
  c.arc(
    cx,
    faceY - 28,
    30,
    15,
    math.Pi,
    math.Pi * 2
  )

  // Wisdom mark
  c.fill(
    255,
    198,
    55
  )

  c.noStroke()

  c.ellipse(
    cx,
    faceY + 25,
    8,
    12
  )

  c.fill(
    255,
    245,
    180
  )

  c.ellipse(
    cx,
    faceY + 26,
    3,
    6
  )

  // Meditation hands
  c.stroke(
    194,
    128,
    92
  )

  c.strokeWeight(8)

  c.line(
    cx - 42,
    165,
    cx - 15,
    150
  )

  c.line(
    cx + 42,
    165,
    cx + 15,
    150
  )

  c.fill(
    207,
    148,
    108
  )

  c.noStroke()

  c.ellipse(
    cx,
    151,
    62,
    22
  )
}

// ============================================================
// GOLDEN PARTICLES
// ============================================================

def drawGoldenParticles(
  c: CanvasDraw
): Unit = {

  particles.foreach { p =>

    val px =
      cwidth / 2.0 +
      math.cos(p.angle) * p.radius

    val py =
      300.0 +
      math.sin(p.angle) *
      p.radius *
      0.72

    val pulse =
      p.size +
      math.abs(
        math.sin(
          sparklePhase +
          p.angle
        )
      ) * 2.0

    c.fill(
      255,
      220,
      95,
      180
    )

    c.noStroke()

    c.ellipse(
      px,
      py,
      pulse,
      pulse
    )
  }
}

// ============================================================
// KNOWLEDGE WORDS
// ============================================================

def drawKnowledgeWords(
  c: CanvasDraw
): Unit = {

  wordParticles.foreach { p =>

    val alpha =
      (
        45 +
        math.abs(
          math.sin(
            t + p.phase
          )
        ) * 120
      ).toInt

    c.fill(
      255,
      225,
      135,
      alpha
    )

    c.noStroke()

    c.text(
      words(p.index),
      p.x,
      p.y
    )
  }
}

// ============================================================
// LOTUS
// ============================================================

def drawLotus(
  c: CanvasDraw
): Unit = {

  val cx =
    cwidth / 2.0

  val base =
    67.0

  val open =
    math.sin(
      lotusT
    ) * 3.0

  // Reflection
  c.fill(
    255,
    190,
    70,
    25
  )

  c.noStroke()

  c.ellipse(
    cx,
    35,
    200,
    26
  )

  // petals
  for (i <- 0 until 11) {

    val angle =
      -90 +
      i * 18

    val rad =
      math.toRadians(
        angle
      )

    val px =
      cx +
      math.cos(rad) * 48

    val py =
      base +
      math.sin(rad) * 20

    c.fill(
      245,
      185,
      75
    )

    c.stroke(
      205,
      132,
      45
    )

    c.strokeWeight(1.5)

    c.ellipse(
      px,
      py,
      48 + open,
      82
    )
  }

  // center
  c.fill(
    255,
    215,
    100
  )

  c.noStroke()

  c.ellipse(
    cx,
    base + 4,
    55,
    72
  )

  c.fill(
    255,
    242,
    175
  )

  c.ellipse(
    cx,
    base + 20,
    21,
    32
  )
}

// ============================================================
// MEDITATION WAVE
// ============================================================

def drawMeditationWave(
  c: CanvasDraw
): Unit = {

  if (!meditation)
    return

  val r =
    100 +
    math.abs(
      math.sin(
        glowT
      )
    ) * 40

  c.noFill()

  c.stroke(
    255,
    230,
    155,
    110
  )

  c.strokeWeight(2)

  c.ellipse(
    cwidth / 2.0,
    300,
    r * 2,
    r * 1.25
  )

  c.stroke(
    255,
    245,
    190,
    50
  )

  c.ellipse(
    cwidth / 2.0,
    300,
    r * 1.45,
    r * 0.9
  )
}

// ============================================================
// COMPLETE SCENE
// ============================================================

def drawScene(
  c: CanvasDraw
): Unit = {

  drawSky(c)

  drawMountains(c)

  drawWater(c)

  drawTree(c)

  drawSun(c)

  drawKnowledgeRays(c)

  drawHalo(c)

  drawBuddha(c)

  drawGoldenParticles(c)

  drawKnowledgeWords(c)

  drawLotus(c)

  drawMeditationWave(c)

  c.fill(
    255,
    225,
    145
  )

  c.noStroke()

  c.text(
    "WISDOM  PEACE  KNOWLEDGE",
    cwidth / 2.0 - 105,
    cheight - 30
  )
}

// ============================================================
// MAIN ANIMATION
// ============================================================

animateWithCanvasDraw { c =>

  t += 0.04

  rayT += 0.35

  glowT += 0.05

  waterT += 0.07

  lotusT += 0.035

  sparklePhase += 0.05

  updateParticles()

  drawScene(c)

  if (isMousePressed) {
    meditation = true
  }
}