-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLavaLamp.swift
168 lines (143 loc) · 6.14 KB
/
LavaLamp.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// LavaLamp.swift
// LavaLamp
//
// Created by InnAppsCoding
// Copyright InnAppsCoding. All rights reserved.
//
import SwiftUI
class BindableData: ObservableObject {
@Published var props: NSMutableDictionary = [:]
}
struct LavaLamp: View {
@ObservedObject var data = BindableData()
@State private var upperBlobSystem: BlobSystem
@State private var lowerBlobSystem: BlobSystem
let blobSize: CGFloat
init() {
let size: CGFloat = 200 // Change size here
self.upperBlobSystem = BlobSystem(size: size)
self.lowerBlobSystem = BlobSystem(size: size)
self.blobSize = size
print("here")
}
var body: some View {
ZStack {
TimelineView(.animation) { timeline in
let time = timeline.date.timeIntervalSinceReferenceDate
gradientColor.mask {
Canvas { context, canvasSize in
upperBlobSystem.update(date: time)
lowerBlobSystem.update(date: time)
context.addFilter(.alphaThreshold(min: 0.3))
context.addFilter(.blur(radius: blobSize / 10))
context.drawLayer { layer in
if let resolvedSymbol = context.resolveSymbol(id: 0) {
layer.draw(resolvedSymbol, at: CGPoint(x: canvasSize.width / 2, y: canvasSize.height / 2 - blobSize * 1.1))
}
for id in upperBlobSystem.blobs.map({ $0.id }) {
if let resolvedSymbol = context.resolveSymbol(id: id) {
layer.draw(resolvedSymbol, at: CGPoint(x: canvasSize.width / 2, y: canvasSize.height / 2 - blobSize * 1.1))
}
}
if let resolvedSymbol = context.resolveSymbol(id: 1) {
layer.draw(resolvedSymbol, at: CGPoint(x: canvasSize.width / 2, y: canvasSize.height / 2 + (blobSize * 0.6)))
}
for id in lowerBlobSystem.blobs.map({ $0.id }) {
if let resolvedSymbol = context.resolveSymbol(id: id) {
layer.draw(resolvedSymbol, at: CGPoint(x: canvasSize.width / 2, y: canvasSize.height / 2 + (blobSize * 0.6)))
}
}
}
} symbols: {
ForEach(0..<2) { i in
Capsule()
.frame(width: blobSize * 0.75, height: blobSize * 0.125)
.tag(i)
}
ForEach(upperBlobSystem.blobs, id: \.self) { blob in
circle(blob)
.rotationEffect(.degrees(180))
}
ForEach(lowerBlobSystem.blobs, id: \.self) { blob in
circle(blob)
}
}
}
.mask(lavaLampImage(filled: true))
}
lavaLampImage(filled: false)
}
.background(Color.black)
// .frame(width: 414, height: 950)
.preferredColorScheme(.dark)
}
func lavaLampImage(filled: Bool) -> some View {
Image("lava.lamp" + (filled ? ".fill" : ""))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: blobSize)
}
var gradientColor: some View {
let gradientTopColor = data.props.value(forKey: "gradientTopColor") as? UIColor
let gradientMiddleColor = data.props.value(forKey: "gradientMiddleColor") as? UIColor
let gradientBottomColor = data.props.value(forKey: "gradientBottomColor") as? UIColor
return Rectangle().fill(LinearGradient(colors: [(gradientTopColor != nil) ? Color(gradientTopColor!) : .purple, (gradientMiddleColor != nil) ? Color(gradientMiddleColor!) : .purple, (gradientBottomColor != nil) ? Color(gradientBottomColor!) : .pink], startPoint: .top, endPoint: .bottom))
}
private func circle(_ blob: Blob) -> some View {
Circle()
.frame(width: blobSize * 0.2, height: blobSize * 0.2)
.offset(x: blob.x, y: blob.y)
.tag(blob.id)
}
}
private struct Blob: Hashable {
let id = UUID()
var x: Double
var y: Double
let yOffset: CGFloat
let creationDate = Date.now.timeIntervalSinceReferenceDate
}
private class BlobSystem {
let size: CGFloat
var blobs: [Blob] = []
init(size: CGFloat) {
self.size = size
newBlobTimer()
}
func update(date: TimeInterval) {
let deathDate = date - (size * 0.06)
for i in 0..<blobs.count {
blobs[i].y -= blobs[i].yOffset
}
for blob in blobs {
if blob.creationDate < deathDate {
blobs.removeAll(where: { $0.id == blob.id })
}
}
}
private func newBlobTimer() {
addNewBlobs()
Timer.scheduledTimer(withTimeInterval: 3.5, repeats: true) { _ in
self.addNewBlobs()
}
}
private func addNewBlobs() {
for _ in 0..<Int.random(in: 0...2) {
let randomX = CGFloat.random(in: -(size * 0.35)...(size * 0.35))
let randomY = CGFloat.random(in: 0...(size * 0.25))
let randomYOffset = CGFloat.random(in: (size * 0.003)...(size * 0.01))
self.blobs.append(Blob(x: randomX, y: randomY, yOffset: randomYOffset))
self.blobs.append(
Blob(x: randomX + CGFloat.random(in: -(size * 0.15)...(size * 0.15)),
y: randomY + CGFloat.random(in: 0...(size * 0.3)),
yOffset: randomYOffset)
)
}
}
}
struct LavaLamp_Previews: PreviewProvider {
static var previews: some View {
LavaLamp()
}
}