Code Sketch


bhakti rane
By: Mhalsakant School
Category: Art
clear()
originBottomLeft()

// ============================================================
// AI THINKING ROBOT
// FUTURE ARTIFICIAL INTELLIGENCE ANIMATION
// ============================================================

var t = 0.0
var learning = false
var learnTimer = 0.0
var messageIndex = 0
var messageTimer = 0.0

val messages = Array(
  "THINKING...",
  "ANALYZING DATA...",
  "LEARNING...",
  "CREATING IDEA...",
  "AI READY!"
)

// ============================================================
// BACKGROUND
// ============================================================

def drawBackground(
    canvas: CanvasDraw
): Unit = {

    canvas.background(
        7,
        12,
        30
    )

    // Grid

    canvas.stroke(
        25,
        55,
        90,
        100
    )

    canvas.strokeWeight(1)

    var gx = 0.0

    while (gx < cwidth) {

        canvas.line(
            gx,
            0,
            gx,
            cheight
        )

        gx += 40
    }

    var gy = 0.0

    while (gy < cheight) {

        canvas.line(
            0,
            gy,
            cwidth,
            gy
        )

        gy += 40
    }
}

// ============================================================
// FLOATING DATA
// ============================================================

def drawData(
    canvas: CanvasDraw
): Unit = {

    for (i <- 0 until 35) {

        val x =
            (i * 73 + t * (15 + i % 4)) % cwidth

        val y =
            (i * 41 + math.sin(t + i) * 30) % cheight

        canvas.fill(
            40,
            210,
            255,
            130
        )

        canvas.noStroke()

        canvas.ellipse(
            x,
            y,
            3,
            3
        )
    }
}

// ============================================================
// AI BRAIN
// ============================================================

def drawBrain(
    canvas: CanvasDraw
): Unit = {

    val cx = cwidth / 2.0
    val cy = cheight - 135.0

    // Brain glow

    val glow =
        90.0 +
        math.sin(t * 4.0) * 15.0

    canvas.fill(
        50,
        180,
        255,
        35
    )

    canvas.noStroke()

    canvas.ellipse(
        cx,
        cy,
        glow + 80,
        glow + 50
    )

    // Neural nodes

    for (i <- 0 until 18) {

        val angle =
            i * 20.0 +
            t * 25.0

        val rad =
            math.toRadians(angle)

        val rx =
            45.0 +
            (i % 4) * 12.0

        val ry =
            28.0 +
            (i % 3) * 10.0

        val nx =
            cx +
            math.cos(rad) * rx

        val ny =
            cy +
            math.sin(rad) * ry

        // Connections

        canvas.stroke(
            60,
            220,
            255,
            150
        )

        canvas.strokeWeight(1)

        canvas.line(
            cx,
            cy,
            nx,
            ny
        )

        // Neuron

        canvas.fill(
            80,
            240,
            255
        )

        canvas.noStroke()

        canvas.ellipse(
            nx,
            ny,
            7,
            7
        )
    }

    // Brain center

    canvas.fill(
        130,
        80,
        255
    )

    canvas.ellipse(
        cx,
        cy,
        20,
        20
    )

    canvas.fill(
        255,
        255,
        255
    )

    canvas.ellipse(
        cx,
        cy,
        7,
        7
    )
}

// ============================================================
// AI ROBOT
// ============================================================

def drawRobot(
    canvas: CanvasDraw
): Unit = {

    val cx = cwidth / 2.0
    val cy = 225.0

    val breathe =
        math.sin(t * 2.0) * 3.0

    // ========================================================
    // HEAD GLOW
    // ========================================================

    canvas.fill(
        40,
        200,
        255,
        35
    )

    canvas.noStroke()

    canvas.ellipse(
        cx,
        cy + breathe,
        190,
        145
    )

    // ========================================================
    // HEAD
    // ========================================================

    canvas.fill(
        55,
        70,
        90
    )

    canvas.stroke(
        80,
        230,
        255
    )

    canvas.strokeWeight(3)

    canvas.rect(
        cx - 80,
        cy - 55 + breathe,
        160,
        115
    )

    // ========================================================
    // FACE SCREEN
    // ========================================================

    canvas.fill(
        5,
        20,
        40
    )

    canvas.noStroke()

    canvas.rect(
        cx - 65,
        cy - 35 + breathe,
        130,
        70
    )

    // ========================================================
    // EYES
    // ========================================================

    val blink =
        math.sin(t * 1.7)

    if (blink > -0.92) {

        canvas.fill(
            70,
            240,
            255
        )

        canvas.ellipse(
            cx - 30,
            cy + breathe,
            20,
            14
        )

        canvas.ellipse(
            cx + 30,
            cy + breathe,
            20,
            14
        )

    } else {

        canvas.stroke(
            70,
            240,
            255
        )

        canvas.strokeWeight(3)

        canvas.line(
            cx - 40,
            cy + breathe,
            cx - 20,
            cy + breathe
        )

        canvas.line(
            cx + 20,
            cy + breathe,
            cx + 40,
            cy + breathe
        )
    }

    // ========================================================
    // MOUTH
    // ========================================================

    canvas.noFill()

    canvas.stroke(
        80,
        230,
        255
    )

    canvas.strokeWeight(2)

    val mouthWidth =
        30.0 +
        math.abs(
            math.sin(t * 8.0)
        ) * 18.0

    canvas.line(
        cx - mouthWidth,
        cy - 20 + breathe,
        cx + mouthWidth,
        cy - 20 + breathe
    )

    // ========================================================
    // BODY
    // ========================================================

    canvas.fill(
        45,
        60,
        80
    )

    canvas.stroke(
        80,
        230,
        255
    )

    canvas.strokeWeight(3)

    canvas.rect(
        cx - 70,
        cy - 145 + breathe,
        140,
        80
    )

    // AI core

    val coreSize =
        18.0 +
        math.sin(t * 5.0) * 4.0

    canvas.fill(
        80,
        240,
        255
    )

    canvas.noStroke()

    canvas.ellipse(
        cx,
        cy - 105 + breathe,
        coreSize,
        coreSize
    )
}

// ============================================================
// AI HANDS
// ============================================================

def drawHands(
    canvas: CanvasDraw
): Unit = {

    val cx = cwidth / 2.0
    val cy = 150.0

    val movement =
        math.sin(t * 3.0) * 12.0

    canvas.stroke(
        70,
        220,
        255
    )

    canvas.strokeWeight(5)

    // Left arm

    canvas.line(
        cx - 70,
        cy,
        cx - 125,
        cy + movement
    )

    // Right arm

    canvas.line(
        cx + 70,
        cy,
        cx + 125,
        cy - movement
    )

    // Hands

    canvas.fill(
        80,
        220,
        255
    )

    canvas.noStroke()

    canvas.ellipse(
        cx - 125,
        cy + movement,
        18,
        18
    )

    canvas.ellipse(
        cx + 125,
        cy - movement,
        18,
        18
    )
}

// ============================================================
// AI MESSAGE
// ============================================================

def drawMessage(
    canvas: CanvasDraw
): Unit = {

    val message =
        messages(messageIndex)

    canvas.fill(
        15,
        30,
        55
    )

    canvas.stroke(
        70,
        230,
        255
    )

    canvas.strokeWeight(2)

    canvas.rect(
        cwidth / 2.0 - 100,
        55,
        200,
        35
    )

    canvas.fill(
        100,
        240,
        255
    )

    canvas.noStroke()

    canvas.text(
        message,
        cwidth / 2.0 - 65,
        78
    )
}

// ============================================================
// LEARNING EFFECT
// ============================================================

def drawLearning(
    canvas: CanvasDraw
): Unit = {

    if (!learning)
        return

    for (i <- 0 until 20) {

        val angle =
            i * 18.0 +
            t * 150.0

        val rad =
            math.toRadians(angle)

        val radius =
            100.0 +
            math.sin(t * 5.0 + i) * 30.0

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

        val py =
            210.0 +
            math.sin(rad) *
            radius * 0.6

        canvas.fill(
            255,
            200,
            60
        )

        canvas.noStroke()

        canvas.ellipse(
            px,
            py,
            6,
            6
        )
    }
}

// ============================================================
// TITLE
// ============================================================

def drawTitle(
    canvas: CanvasDraw
): Unit = {

    canvas.fill(
        220,
        245,
        255
    )

    canvas.noStroke()

    canvas.text(
        "ARTIFICIAL INTELLIGENCE",
        cwidth / 2.0 - 110,
        cheight - 30
    )

    canvas.fill(
        70,
        220,
        255
    )

    canvas.text(
        "THE MACHINE THAT LEARNS",
        cwidth / 2.0 - 100,
        cheight - 50
    )

    canvas.fill(
        255,
        220,
        80
    )

    canvas.text(
        "CLICK TO START LEARNING",
        cwidth / 2.0 - 95,
        25
    )
}

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

def drawScene(
    canvas: CanvasDraw
): Unit = {

    drawBackground(canvas)

    drawData(canvas)

    drawBrain(canvas)

    drawRobot(canvas)

    drawHands(canvas)

    drawLearning(canvas)

    drawMessage(canvas)

    drawTitle(canvas)
}

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

animateWithCanvasDraw { canvas =>

    t += 0.04

    // Change AI message

    messageTimer += 0.04

    if (messageTimer > 2.5) {

        messageTimer = 0.0

        messageIndex += 1

        if (messageIndex >= messages.length) {
            messageIndex = 0
        }
    }

    // Learning mode

    if (learning) {

        learnTimer += 0.04

        if (learnTimer > 5.0) {

            learning = false
            learnTimer = 0.0
        }
    }

    // Mouse click

    if (isMousePressed) {

        learning = true
        learnTimer = 0.0
    }

    drawScene(canvas)
}