2021-07-04 19:42:03 +03:00
|
|
|
|
// функции рисоввания
|
|
|
|
|
|
2021-07-08 01:38:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* @param {Object} canvas canvas object
|
|
|
|
|
* @param {String} color строка с цветом (HEX, либо имя, либо rgba, etc)
|
|
|
|
|
*/
|
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
|
|
|
|
|
2021-07-08 00:01:20 +03:00
|
|
|
|
export function placeImage(canvas, area, imageFile, colors) {
|
2021-07-06 09:45:08 +03:00
|
|
|
|
let context = canvas.getContext('2d');
|
|
|
|
|
|
2021-07-08 00:01:20 +03:00
|
|
|
|
let image = new Image();
|
|
|
|
|
image.src = `assets/images/${imageFile}`;
|
|
|
|
|
|
2021-07-07 13:59:12 +03:00
|
|
|
|
context.strokeStyle = colors.buttonStroke;
|
|
|
|
|
context.lineWidth = colors.strokeSize;
|
|
|
|
|
|
2021-07-06 09:45:08 +03:00
|
|
|
|
if (image.width >= image.height) {
|
|
|
|
|
if (image.width > area.w) {
|
|
|
|
|
let newImageW = area.w;
|
2021-07-07 13:59:12 +03:00
|
|
|
|
let newImageH = image.height / (image.width / area.w);
|
|
|
|
|
|
|
|
|
|
context.drawImage(image, getCenterH(canvas.width, newImageW), area.y, newImageW, newImageH);
|
|
|
|
|
context.strokeRect(getCenterH(canvas.width, newImageW), area.y, newImageW, newImageH);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
// TODO: getCenterV({this}) <= need plus progressBar sizes
|
2021-07-07 14:55:10 +03:00
|
|
|
|
context.drawImage(image, getCenterH(canvas.width, image.width), getCenterV(canvas.height / 2 + area.y, image.height), image.width, image.height);
|
|
|
|
|
context.strokeRect(getCenterH(canvas.width, image.width), getCenterV(canvas.height / 2 + area.y, image.height), image.width, image.height);
|
2021-07-06 09:45:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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-07 13:59:12 +03:00
|
|
|
|
context.strokeRect(getCenterH(canvas.width, newImageW), area.y, newImageW, newImageH);
|
2021-07-06 09:45:08 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 01:38:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Возвращает X координату для центрировки объекта
|
|
|
|
|
* @param {Integer} canvasWidth
|
|
|
|
|
* @param {Integer} objectWidth
|
|
|
|
|
* @return {Integer} X координата
|
|
|
|
|
*/
|
2021-07-04 21:07:22 +03:00
|
|
|
|
export function getCenterH(canvasWidth, objectWidth) {
|
|
|
|
|
return canvasWidth / 2 - objectWidth / 2;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 01:38:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Возвращает Y координату для центрировки объекта
|
|
|
|
|
* @param {Integer} canvasHeight
|
|
|
|
|
* @param {Integer} objectHeight
|
|
|
|
|
* @return {Integer} Y координата
|
|
|
|
|
*/
|
2021-07-04 21:07:22 +03:00
|
|
|
|
export function getCenterV(canvasHeight, objectHeight) {
|
|
|
|
|
return canvasHeight / 2 - objectHeight / 2;
|
|
|
|
|
}
|
2021-07-08 00:01:20 +03:00
|
|
|
|
|
|
|
|
|
export function drawGameAreas(context, areas) {
|
|
|
|
|
context.strokeStyle = "#ff0000";
|
|
|
|
|
context.lineWidth = 1;
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(areas)) {
|
|
|
|
|
context.strokeRect(value.x, value.y, value.w, value.h);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function drawInfo(canvas, landscape_orientation) {
|
|
|
|
|
let cW = canvas.width;
|
|
|
|
|
let cH = canvas.height;
|
|
|
|
|
let context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
|
|
console.log("INFO");
|
|
|
|
|
|
|
|
|
|
context.fillStyle = 'rgba(0,0,0,.8)';
|
|
|
|
|
context.fillRect(0, 0, cW, cH);
|
|
|
|
|
|
|
|
|
|
// if(!landscape_orientation) {
|
|
|
|
|
context.fillStyle = '#000000';
|
|
|
|
|
context.fillRect(80, 80, cW - 160, cH - 160);
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-08 01:38:10 +03:00
|
|
|
|
/**
|
|
|
|
|
* Рисует полосу прогресса с визуализацией правильных и неправильных ответов
|
|
|
|
|
* @param {} context
|
|
|
|
|
* @param {Object} area зона в которой будет отрисовываться объект
|
|
|
|
|
* @param {Object} colors объект цветов
|
|
|
|
|
* @param {Object} questions объект вопросов
|
|
|
|
|
*/
|
2021-07-08 00:01:20 +03:00
|
|
|
|
export function drawProgressBar(context, area, colors, questions) {
|
|
|
|
|
let sizeProgressItem = area.w / questions.length;
|
|
|
|
|
|
|
|
|
|
context.strokeStyle = colors.strokeSize;
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < questions.length; i++) {
|
|
|
|
|
switch (questions[i].status) {
|
|
|
|
|
case null:
|
|
|
|
|
context.fillStyle = colors.answer.notPassed;
|
|
|
|
|
break;
|
|
|
|
|
case true:
|
|
|
|
|
context.fillStyle = colors.answer.right;
|
|
|
|
|
break;
|
|
|
|
|
case false:
|
|
|
|
|
context.fillStyle = colors.answer.wrong;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.fillRect(10 + (i * sizeProgressItem), 10, sizeProgressItem, 20);
|
|
|
|
|
context.strokeRect(10 + (i * sizeProgressItem), 10, sizeProgressItem, 20);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function drawQuestionLabel(canvas, area, questLabel) {
|
|
|
|
|
let context = canvas.getContext('2d');
|
|
|
|
|
|
|
|
|
|
context.font = '32px Yanone Kaffeesatz';
|
|
|
|
|
context.textAlign = 'center';
|
|
|
|
|
context.fillStyle = '#ffffff';
|
|
|
|
|
|
|
|
|
|
context.fillText(questLabel, canvas.width / 2, area.y + 30);
|
|
|
|
|
}
|