sakuya/main.js

42 lines
1011 B
JavaScript
Raw Permalink Normal View History

2022-04-10 00:37:53 +08:00
import Matrix from './widgets/matrix.js'
2023-07-23 14:32:28 +08:00
const matrix = new Matrix(32, 32)
2022-04-10 00:37:53 +08:00
document.body.appendChild(matrix.element)
// DEMO
2023-07-23 14:32:28 +08:00
const canvas = document.createElement("canvas")
const context = canvas.getContext('2d')
const image = new Image()
2022-04-10 00:37:53 +08:00
2023-07-23 14:32:28 +08:00
image.src = 'img/00010-938548211.png'
image.onload = function () {
context.drawImage(image, 0, 0)
2022-04-10 00:37:53 +08:00
2023-07-23 14:32:28 +08:00
//获取图片数据对象
let imgData = context.getImageData(0, 0, canvas.width, canvas.height)
2022-04-10 00:37:53 +08:00
// 分离通道
let R = []
let G = []
let B = []
for (var i = 0; i < imgData.data.length; i += 4) {
R.push(imgData.data[i])
G.push(imgData.data[i + 1])
B.push(imgData.data[i + 2])
}
// 使用单通道
for (var i = 0; i < imgData.data.length; i += 4) {
imgData.data[i + 1] = 0
imgData.data[i + 2] = 0
}
context.putImageData(imgData, 0, 0);
2023-07-23 14:32:28 +08:00
// 将图像展示到矩阵中
for (var i = 0; i < imgData.data.length && i < 4096; i += 4) {
matrix.setItem(i / 4, 0, imgData.data[i])
2022-04-10 00:37:53 +08:00
}
2024-08-11 20:04:07 +08:00
}
2022-04-10 00:37:53 +08:00
document.body.appendChild(canvas)