from processing.core import PVector from math import sqrt, radians, degrees angle = 0 scale_val = sqrt(0.5) currentEventIndex = 0 eventStartTime = 0 currentZoom = 1.0 maxDepth = 13 class Event: def __init__(self, targetAngle, targetScale, targetZoom, duration, isPause, changeScale, changeZoom): self.targetAngle = targetAngle self.targetScale = targetScale self.targetZoom = targetZoom self.duration = duration self.isPause = isPause self.changeScale = changeScale self.changeZoom = changeZoom events = [ Event(0, sqrt(0.5), 1, 1000, False, False, False), Event(45, sqrt(0.5), 1, 3000, False, False, False), Event(45, sqrt(0.5), 1, 1000, True, False, False), Event(60, sqrt(0.5), 1, 5000, False, False, False), Event(60, sqrt(0.5), 1, 1000, True, False, False), Event(90, sqrt(0.5), 1, 8000, False, False, False), Event(90, sqrt(0.5), 1, 5000, True, False, False), Event(60, sqrt(0.5), 1, 8000, False, False, False), Event(60, sqrt(0.5), 1, 1000, True, False, False), Event(60, 0.89, 0.5, 4000, False, True, True), Event(60, 0.89, 0.25, 5000, False, True, True), ] cycleDuration = 5000 cycleColors = [ color(0, 0, 255), # Blue color(128, 0, 128), # Purple color(255, 192, 203), # Pink color(255, 165, 0), # Orange color(255, 255, 0), # Yellow color(0, 128, 0), # Green color(64, 224, 208), # Turquoise color(0, 0, 255), # Blue ] def setup(): global eventStartTime size(800, 800) colorMode(RGB, 255, 255, 255) background(0) eventStartTime = millis() def draw(): global angle, scale_val, currentEventIndex, eventStartTime, currentZoom background(0) if currentEventIndex < len(events): currentEvent = events[currentEventIndex] elapsedTime = millis() - eventStartTime if currentEvent.changeScale: startScale = scale_val if currentEventIndex == 0 else events[currentEventIndex - 1].targetScale t = map(elapsedTime, 0, currentEvent.duration, 0, 1) scale_val = lerp(startScale, currentEvent.targetScale, t) if currentEvent.changeZoom: startZoom = currentZoom if currentEventIndex == 0 else events[currentEventIndex - 1].targetZoom t = map(elapsedTime, 0, currentEvent.duration, 0, 1) currentZoom = lerp(startZoom, currentEvent.targetZoom, t) if not currentEvent.isPause: startAngle = angle if currentEventIndex == 0 else radians(events[currentEventIndex - 1].targetAngle) t = map(elapsedTime, 0, currentEvent.duration, 0, 1) angle = lerp(startAngle, radians(currentEvent.targetAngle), t) if elapsedTime >= currentEvent.duration: angle = radians(currentEvent.targetAngle) scale_val = currentEvent.targetScale currentZoom = currentEvent.targetZoom currentEventIndex += 1 eventStartTime = millis() pushMatrix() translate(width / 2, height / 2) scale(currentZoom) translate(-width / 2, -height / 2) start = PVector(width / 2, height / 2) v = PVector(0, -height / 7) rec(start, v, scale_val, angle, maxDepth, 0) v_mirrored = PVector(0, height / 7) rec(start, v_mirrored, scale_val, angle, maxDepth, 0) popMatrix() fill(255) textSize(16) textAlign(RIGHT) roundedAngle = round(degrees(angle), 3) roundedScale = round(scale_val, 3) text("Angle: " + str(roundedAngle) + "°", width - 10, 20) text("Scale: " + str(roundedScale), width - 10, 40) text("Zoom: " + str(round(currentZoom, 2)), width - 10, 60) if (frameCount <= 320): saveFrame("frames/frame-####.png") def rec(start, v, sc, a, count, currentDepth): if count <= 0: return end = PVector(start.x + v.x, start.y + v.y) currentColor = getColorForDepth(currentDepth, maxDepth) stroke(currentColor) strokeWeight(map(currentDepth, 0, maxDepth, 1, 0.5)) line(start.x, start.y, end.x, end.y) new_v = PVector(v.x, v.y) new_v.rotate(a) new_v.mult(sc) rec(end, new_v, sc, a, count - 1, currentDepth + 1) new_v = PVector(v.x, v.y) new_v.rotate(-a) new_v.mult(sc) rec(end, new_v, sc, -a, count - 1, currentDepth + 1) def sumDurations(currentIndex): total = 0 for i in range(currentIndex + 1): total += events[i].duration return total def getColorForDepth(depth, maxDepth): baseCycleProgress = (millis() % cycleDuration) / float(cycleDuration) depthOffset = float(depth) / maxDepth cycleProgress = (baseCycleProgress - depthOffset) % 1.0 if cycleProgress < 0: cycleProgress += 1.0 colorIndex = int(cycleProgress * (len(cycleColors) - 1)) lerpFactor = (cycleProgress * (len(cycleColors) - 1)) - colorIndex startColor = cycleColors[colorIndex] endColor = cycleColors[(colorIndex + 1) % len(cycleColors)] return lerpColor(startColor, endColor, lerpFactor)