xxxxxxxxxx
215
let time = 0
let Fpixels = [] // 0 = empty pixel, {0-8, 0-8, 0-8} = colored pixel
let FPixelSize = 4
let FCanvasHeight
let FCanvasWidth
let Pr = 0
let Pg = 0
let Pb = 0
let Perase = false
let selectorX = 0
let selectorY = 0
function setup() {
createCanvas(512, 512);
FCanvasHeight = height/FPixelSize
FCanvasWidth = width/FPixelSize
for(let i = 0; i < FCanvasWidth*FCanvasHeight; i++){
Fpixels[i] = 0
}
}
function Pfill(r,g,b){
Pr = r
Pg = g
Pb = b
}
function pixel(x, y){
if(!Perase){
Fpixels[(y*FCanvasWidth) + x] = [Pr,Pg,Pb]
}else{
Fpixels[(y * FCanvasWidth) + x] = 0
}
}
function pixelCol(x, y, r, g, b){
if(!Perase){
Fpixels[(y*FCanvasWidth) + x] = [r,g,b]
}else{
Fpixels[(y * FCanvasWidth) + x] = 0
}
}
function pixelLine(x1, y1, x2, y2){
x1 = min(FCanvasWidth - 1, x1)
x2 = min(FCanvasWidth - 1, x2)
y1 = min(FCanvasHeight - 1, y1)
y2 = min(FCanvasHeight - 1, y2)
pixel(x1,y1)
pixel(x2,y2)
let distance = sqrt((x2-x1)**2 + (y2-y1)**2)
let x = (x2 - x1)
let y = (y2 - y1)
let angle = atan(y/x)
if(x > 0) angle = angle + PI
angle += PI
if(abs(x) < 1) angle += PI
for(let i = 0; i <= floor(distance); i++){
let Px = x1 + 0.5 + (cos(angle) * i)
let Py = y1 + 0.5 + (sin(angle) * i)
pixel(floor(Px), floor(Py))
}
}
function pixelRect(x, y, w, h){
for(let a = 0; a < h; a++){
for(let b = 0; b < w; b++){
pixel(x + b, y + a)
}
}
}
function pixelLineRect(x, y, w, h){
pixelLine(x, y, x + w, y)
pixelLine(x, y, x, y + h)
pixelLine(x + w, y + h, x, y + h)
pixelLine(x + w, y + h, x + w, y)
}
// for funsies
function pixelLineGradient(x1, y1, x2, y2, col1, col2){
x1 = min(FCanvasWidth - 1, x1)
x2 = min(FCanvasWidth - 1, x2)
y1 = min(FCanvasHeight - 1, y1)
y2 = min(FCanvasHeight - 1, y2)
pixel(x1,y1)
pixel(x2,y2)
let distance = sqrt((x2-x1)**2 + (y2-y1)**2)
let x = (x2 - x1)
let y = (y2 - y1)
let angle = atan(y/x)
if(x > 0) angle = angle + PI
angle += PI
if(abs(x) < 1) angle += PI
let rdiff = col2[0] - col1[0]
let gdiff = col2[1] - col1[1]
let bdiff = col2[2] - col1[2]
for(let i = 0; i <= floor(distance); i++){
let Px = x1 + 0.5 + (cos(angle) * i)
let Py = y1 + 0.5 + (sin(angle) * i)
pixelCol(floor(Px), floor(Py), col1[0] + round(rdiff * (i/distance) ), col1[1] + round(gdiff * (i/distance) ), col1[2] + round(bdiff * (i/distance) ))
}
}
function keyPressed(){
if(key == "a"){
selectorX += -1
}
if(key == "d"){
selectorX += 1
}
if(key == "w"){
selectorY += -1
}
if(key == "s"){
selectorY += 1
}
}
function draw() {
background(220);
time += round(deltaTime/1000,3)
time = round(time,3)
let Mx = floor(mouseX/FPixelSize)
let My = floor(mouseY/FPixelSize)
// waves
for(let i = 0; i < FCanvasWidth; i++){
let h = 64 - (sin(i/8 + (time/(2*PI))*5) * ((sin(i/48 + (time/(2*PI))*6))*2 + 4))
pixelLineGradient(i,127,i,h,[0,0,2],[0,0,5])
}
/*
Pfill(0,5,7)
if(floor(time)%2 == 1){
pixelLineRect(59 + selectorX*12,59 + selectorY*12,10,10)
}else{
pixelLineRect(60 + selectorX*12,60 + selectorY*12,8,8)
}
*/
// draw Fpixels
let x = 0
let y = 0
noStroke()
for(let i = 0; i < FCanvasWidth*FCanvasHeight; i++){
if(Fpixels[i] == 0){
}else{
fill(Fpixels[i][0]*32,Fpixels[i][1]*32,Fpixels[i][2]*32)
rect(x*FPixelSize, y*FPixelSize, FPixelSize, FPixelSize)
}
x++
if(x >= FCanvasWidth){
y++
x=0
}
}
// clear
for(let i = 0; i < FCanvasWidth*FCanvasHeight; i++){
Fpixels[i] = 0
}
noStroke()
fill(0)
text("FPS: "+round(1/(deltaTime/1000)), 20, 20)
}