This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import canvas | ||
import constants.{max_x, max_y} | ||
import gleam/float | ||
import gleam/int | ||
import gleam/list | ||
import gleam/result | ||
|
||
pub fn apply_brush( | ||
frame_data: canvas.ImageData, | ||
mouse_x: Int, | ||
mouse_y: Int, | ||
brush_size: Int, | ||
brush_colour: canvas.Pixel, | ||
) -> canvas.ImageData { | ||
list.range(-1 * brush_size, brush_size) | ||
|> list.fold(frame_data, fn(row_data, y) { | ||
list.range(-1 * brush_size, brush_size) | ||
|> list.fold(row_data, fn(pixel_data, x) { | ||
let distance = | ||
float.square_root( | ||
{ | ||
int.power(x, 2.0) | ||
|> result.unwrap(0.0) | ||
} | ||
+. { | ||
int.power(y, 2.0) | ||
|> result.unwrap(0.0) | ||
}, | ||
) | ||
|> result.unwrap(0.0) | ||
|> float.round() | ||
|
||
let new_x = mouse_x + x | ||
let new_y = mouse_y + y | ||
|
||
case | ||
distance > brush_size | ||
|| new_x < 0 | ||
|| new_x > max_x | ||
|| new_y < 0 | ||
|| new_y > max_y | ||
{ | ||
True -> pixel_data | ||
False -> { | ||
pixel_data | ||
|> canvas.set_pixel(new_x, new_y, brush_colour) | ||
} | ||
} | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import canvas | ||
import constants.{max_y} | ||
import gleam/bool | ||
|
||
pub fn collapse_like( | ||
frame_data: canvas.ImageData, | ||
x: Int, | ||
y: Int, | ||
) -> canvas.ImageData { | ||
use <- canvas.ensure_pixel_exists(frame_data, x, y) | ||
use <- bool.guard(when: y == max_y, return: frame_data) | ||
|
||
let pixel_val = | ||
frame_data | ||
|> canvas.get_pixel(x, y) | ||
let below_pixel_val = | ||
frame_data | ||
|> canvas.get_pixel(x, y + 1) | ||
|
||
case pixel_val == below_pixel_val && pixel_val != 0x00000000 { | ||
True -> | ||
frame_data | ||
|> canvas.set_pixel(x, y + 1, 0x00000000) | ||
False -> frame_data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub const max_x = 639 | ||
|
||
pub const max_y = 479 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import canvas | ||
import constants.{max_x, max_y} | ||
import gleam/bool | ||
|
||
pub fn apply_gravity( | ||
frame_data: canvas.ImageData, | ||
x: Int, | ||
y: Int, | ||
next_pixel_offset: Int, | ||
) -> canvas.ImageData { | ||
use <- canvas.ensure_pixel_exists(frame_data, x, y) | ||
use <- bool.guard(when: y == max_y, return: frame_data) | ||
|
||
let pixel_val = | ||
frame_data | ||
|> canvas.get_pixel(x, y) | ||
|
||
use <- bool.guard(when: pixel_val == 0x00000000, return: frame_data) | ||
|
||
let below_pixel_val = | ||
frame_data | ||
|> canvas.get_pixel(x, y + 1) | ||
|
||
case below_pixel_val { | ||
0x00000000 -> | ||
frame_data | ||
|> canvas.set_pixel(x, y + 1, pixel_val) | ||
|> canvas.set_pixel(x, y, 0x00000000) | ||
_ -> { | ||
let next_x = x + next_pixel_offset | ||
|
||
let below_next_pixel_val = { | ||
case next_x > max_x || next_x < 0 { | ||
True -> 0xffffffff | ||
False -> | ||
frame_data | ||
|> canvas.get_pixel(next_x, y + 1) | ||
} | ||
} | ||
|
||
case below_next_pixel_val { | ||
0x00000000 -> | ||
frame_data | ||
|> canvas.set_pixel(next_x, y + 1, pixel_val) | ||
|> canvas.set_pixel(x, y, 0x00000000) | ||
_ -> { | ||
let prev_x = x - next_pixel_offset | ||
|
||
let below_prev_pixel_val = { | ||
case prev_x > max_x || prev_x < 0 { | ||
True -> 0xffffffff | ||
False -> | ||
frame_data | ||
|> canvas.get_pixel(prev_x, y + 1) | ||
} | ||
} | ||
|
||
case below_prev_pixel_val { | ||
0x00000000 -> | ||
frame_data | ||
|> canvas.set_pixel(prev_x, y + 1, pixel_val) | ||
|> canvas.set_pixel(x, y, 0x00000000) | ||
_ -> frame_data | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |