quizEngine/js/draw.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-07-04 19:42:03 +03:00
// функции рисоввания
2021-07-07 01:26:29 +03:00
export function clearContext(canvas, color) {
2021-07-04 19:42:03 +03:00
let cW = canvas.width;
let cH = canvas.height;
let context = canvas.getContext('2d');
2021-07-07 01:26:29 +03:00
context.fillStyle = color;
2021-07-04 19:42:03 +03:00
context.fillRect(0, 0, cW, cH);
}
2021-07-04 21:07:22 +03:00
export function placeImage(canvas, area, image) {
let context = canvas.getContext('2d');
if (image.width >= image.height) {
if (image.width > area.w) {
let newImageW = area.w;
let newImageH = image.height * (area.h / newImageW);
if (newImageH > area.h) {
newImageH = area.h;
// BUG: ...
newImageW = image.width * (area.h / image.height);
context.drawImage(image, getCenterH(canvas.width, newImageW), area.y, newImageW, newImageH);
}
else {
context.drawImage(image, area.x, getCenterV(canvas.height - area.h + area.y, newImageH), newImageW, newImageH);
}
}
}
else {
let newImageH = area.h;
let newImageW = image.width * (area.h / image.height);
context.drawImage(image, getCenterH(canvas.width, newImageW), area.y, newImageW, newImageH);
}
}
2021-07-04 21:07:22 +03:00
export function getCenterH(canvasWidth, objectWidth) {
return canvasWidth / 2 - objectWidth / 2;
}
export function getCenterV(canvasHeight, objectHeight) {
return canvasHeight / 2 - objectHeight / 2;
}