-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sketch.js
186 lines (163 loc) · 5.21 KB
/
sketch.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Main
let scaling
let scalingInverse
const imgW = 2048
const imgH = 858
const creditsText = "© Leonardo C. Bottaro - 2023\nOpen source @ github.com/leocb/CameraSimulator";
//UI
let FstopSlider
let ShutterSlider
let IsoSlider
let WbSlider
let IsoAuto
let uiElementY
let uiLabelY
const uiMargin = 100
const elementsWidth = 100
const elementsSpacing = 40
const FstopXpos = elementsSpacing / 2
const ShutterXpos = FstopXpos + elementsWidth + elementsSpacing
const IsoXpos = ShutterXpos + elementsWidth + elementsSpacing
const WbXpos = IsoXpos + elementsWidth + elementsSpacing
// Values
let currF = F1_4
let currS = S120
let currI = ISO200
let currBgEv = 0
let currWbValue
let currWb
// Overlay
const IsoOverlayOverflow = (3000 - imgW) // ISO overlay image width - main image width
let IsoOverlayGraphics = []
let IsoOverlayXpos = 0
// Zoom
let zoomGraphics
const zoomWindowSize = 200
const zoomScale = 2.5
function setup() {
createCanvas(1, 1);
zoomGraphics = createGraphics(zoomWindowSize / zoomScale, zoomWindowSize / zoomScale);
frameRate(30)
// UI
fill(255)
noStroke()
textSize(16)
textStyle(BOLD)
textAlign(LEFT, CENTER)
FstopSlider = createSlider(0, 2, currF)
FstopSlider.size(elementsWidth)
FstopSlider.elt.addEventListener('input', updateValues)
ShutterSlider = createSlider(0, 2, currS)
ShutterSlider.size(elementsWidth)
ShutterSlider.elt.addEventListener('input', updateValues)
WbSlider = createSlider(2000, 8000, 5000, 200)
WbSlider.size(elementsWidth*2)
WbSlider.elt.addEventListener('input', updateValues)
IsoSlider = createSlider(0, 4, currI)
IsoSlider.size(elementsWidth)
IsoSlider.elt.addEventListener('input', updateValues)
IsoAuto = createCheckbox("AUTO", true)
IsoAuto.changed(() => {
IsoSlider.style('visibility', IsoAuto.checked() ? "hidden" : "visible")
updateValues()
})
positionStuff()
updateValues()
// Load everything
preLoadImages()
}
// update UI when resizing
function windowResized() {
positionStuff()
}
// position UI elements when resizing
function positionStuff() {
scaling = (windowWidth / imgW)
scalingInverse = 1/scaling
resizeCanvas(windowWidth, scaling * imgH + uiMargin);
uiElementY = imgH * scaling + uiMargin / 2;
uiLabelY = uiElementY - 10;
FstopSlider.position(FstopXpos, uiElementY)
ShutterSlider.position(ShutterXpos, uiElementY)
IsoSlider.position(IsoXpos, uiElementY)
WbSlider.position(WbXpos, uiElementY)
IsoAuto.position(IsoXpos, uiElementY - 41)
}
function updateValues() {
currF = FstopSlider.value()
currS = ShutterSlider.value()
currI = IsoSlider.value()
currWbValue = WbSlider.value()
currWb = wbRgbTable[(currWbValue / 200) - 10]
// Update ISO if Auto is enabled
if (IsoAuto.checked()) {
currI = currF + currS
}
// Background EV
currBgEv = (2 - currS) + currI
}
function draw() {
// Wait here until everything is done loading
if (isLoading) {
background(0)
push()
textAlign(CENTER, CENTER)
text(`Downloading: ${Math.round(progress * 100, 2)}%`, width / 2, height / 2)
if (progress >= 0.98) {
text(`Initializing images, please wait...`, width / 2, height / 2 + 20)
}
pop()
return
}
// Move ISO Noise
IsoOverlayXpos = (IsoOverlayXpos + 40 + Math.random() * (IsoOverlayOverflow / 2)) % IsoOverlayOverflow
// MAIN image
push()
scale(scaling)
drawMovie(this, { x: 0, y: 0 })
pop()
// ZOOM image if mouse is pressed
if (mouseY < imgH * scaling) {
// Draw image relative to mouse pos
drawMovie(zoomGraphics, {
x: (-mouseX + (zoomWindowSize / zoomScale) / zoomScale / 2) *scalingInverse,
y: (-mouseY + (zoomWindowSize / zoomScale) / zoomScale / 2) *scalingInverse
})
// Draw zoom window
push()
translate(mouseX - zoomWindowSize - 5, mouseY - zoomWindowSize - 5)
scale(zoomScale)
image(zoomGraphics, 0, 0)
pop()
}
// UI
push()
text(`F-stop: ${fToPretty(currF)}`, FstopXpos, uiLabelY)
text(`Shutter: ${sToPretty(currS)}`, ShutterXpos, uiLabelY)
text(`ISO: ${iToPretty(currI)}`, IsoXpos, uiLabelY)
text(`White Balance: ${currWbValue}K`, WbXpos, uiLabelY)
textSize(14)
textAlign(RIGHT, BOTTOM)
text(creditsText, width - 10, height - 10)
pop()
}
// Open github page when clicking on the credits
function mouseClicked() {
if (mouseY >= height - uiMargin &&
mouseX >= WbXpos + elementsWidth*2 + elementsSpacing) {
window.open("https://github.com/leocb/CameraSimulator");
}
}
// Draw the movie animation with the selected camera settings
function drawMovie(graphics, offset) {
graphics.background(0)
graphics.image(imgBg[currF][currBgEv], offset.x, offset.y);
graphics.image(imgFan[currF][currS][currI][frameCount % 7], offset.x + 255, offset.y + 198);
graphics.blendMode(HARD_LIGHT)
graphics.fill(currWb.r-127, currWb.g-127, currWb.b-127)
graphics.rect(0, 0, imgW, imgH)
graphics.fill(255)
graphics.blendMode(SCREEN)
graphics.image(IsoOverlayGraphics[currI], - IsoOverlayXpos + offset.x, offset.y);
graphics.blendMode(BLEND)
}