const canvas = document.getElementById('canvas');
function loadImage(src) {
return new Promise((resolve, reject) => {
const image = new Image();
image.src = src;
if (image.complete) {
resolve(image);
return;
}
image.onload = () => resolve(image);
image.onerror = (e) => reject(e);
});
}
class Character {
constructor(options) {
this.ctx = options.ctx;
this.image = options.image;
this.width = options.width;
this.height = options.height;
this.render();
}
load() {}
render() {
this.ctx.drawImage(this.image, 0, 0, this.width / 7, this.height, 0, 0, this.width / 7, this.height);
}
}
(async function main() {
const spriteImage = await loadImage('./img/sprite.png');
const character = new Character({
ctx: canvas.getContext('2d'),
image: spriteImage,
width: 448,
height: 60,
});
})();
function rgbaToAndroid1(rgba) {
const c = new Uint8Array([
rgba[2],
rgba[1],
rgba[0],
Math.round(rgba[3] * 255)
]);
return new Uint32Array(c.buffer)[0];
}
// либо
function rgbaToAndroid2(rgba) {
let color = 0;
for (let i = 0; i < 3; i++) {
color += rgba[i] * 2 ** ((2 - i) * 8);
}
color += Math.round(rgba[3] * 255) * 2 ** (3 * 8);
return color;
}
console.log(rgbaToAndroid1([255, 122, 107, 1]));
console.log(rgbaToAndroid2([255, 122, 107, 1]));
function createTable(el) {
for (let i = 0, row; i < el.length; i++) {
if (i % 7 === 0) {
row = document.createElement('tr');
thead.appendChild(row);
}
const cell = document.createElement('th');
cell.innerHTML = el[i];
row.appendChild(cell);
}
}
function checkingReading() {
let prevTimeStamp = performance.now();
chatbox.addEventListener("DOMSubtreeModified", (e) => {
const dt = e.timeStamp - prevTimeStamp;
prevTimeStamp = e.timeStamp;
const changesPerSecond = 1000 / dt;
console.log(changesPerSecond)
});
}
function arrayToList(array) {
const last = array.length - 1;
return {
value: array[last],
rest: last === 0 ? null : arrayToList(array.slice(0, last))
};
}
function arrayToList(array, idx = array.length - 1) {
return {
value: array[idx],
rest: idx === 0 ? null : arrayToList(array, idx - 1)
};
}