Last active
August 22, 2021 13:20
-
-
Save YonatanKra/6b3e42bedfbf9932d279d4c573079e3b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| function normalizeMatrix(matrix) { | |
| const maxValue = matrix.reduce((max, row) => { | |
| return row.reduce((max, value) => Math.max(value, max)); | |
| }, -Infinity); | |
| return matrix.map((row) => { | |
| return row.map((val) => val / maxValue); | |
| }); | |
| } | |
| function draw(matrix, canvas) { | |
| const ctx = canvas.getContext("2d"); | |
| const normalizedMatrix = normalizeMatrix(matrix); | |
| ctx.beginPath(); | |
| normalizedMatrix.forEach((pixelsRow, rowIndex) => { | |
| const y = rowIndex * MATRIX_DIMENSIONS.pixelHeight; | |
| pixelsRow.forEach((pixel, pixelIndex) => { | |
| const x = pixelIndex * MATRIX_DIMENSIONS.pixelWidth; | |
| ctx.fillStyle = getColor(pixel); | |
| ctx.fillRect( | |
| x, | |
| y, | |
| MATRIX_DIMENSIONS.pixelWidth, | |
| MATRIX_DIMENSIONS.pixelHeight | |
| ); | |
| }); | |
| }); | |
| ctx.closePath(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment