Работа с канвасом

This commit is contained in:
Book Pauk
2019-01-16 17:03:58 +07:00
parent 0ff4e963ea
commit 159dbd6860

View File

@@ -1,9 +1,6 @@
<template> <template>
<div ref="main" class="main"> <div ref="main" class="main">
<!--pre>{{ meta }}</pre--> <canvas ref="canvas" class="canvas"></canvas>
<p v-for="item in items" :key="item.id">
<pre>{{ item.text }}</pre>
</p>
</div> </div>
</template> </template>
@@ -42,6 +39,19 @@ class TextPage extends Vue {
}, 100); }, 100);
} }
mounted() {
this.canvas = this.$refs.canvas;
this.context = this.canvas.getContext('2d');
this.context.textAlign = 'left';
}
updateCanvasSize() {
this.canvas.width = this.$refs.main.clientWidth;
this.canvas.height = this.$refs.main.clientHeight;
this.lineHeight = this.fontSize + this.lineInterval;
this.pageLineCount = Math.floor(this.canvas.height/this.lineHeight);
}
showBook() { showBook() {
this.$refs.main.focus(); this.$refs.main.focus();
this.book = null; this.book = null;
@@ -53,6 +63,15 @@ class TextPage extends Vue {
this.linesDown = null; this.linesDown = null;
this.pageLineCount = 0; this.pageLineCount = 0;
//canvas props
this.textColor = 'black';
this.backgroundColor = '#478355';
this.fontStyle = ''; //'bold','italic'
this.fontSize = 20; //px
this.fontName = 'arial';
this.lineInterval = 5; //px
this.updateCanvasSize();
this.drawPage();//пока не загрузили, очистим канвас this.drawPage();//пока не загрузили, очистим канвас
if (this.lastBook) { if (this.lastBook) {
@@ -73,16 +92,11 @@ class TextPage extends Vue {
this.fb2.bookTitle this.fb2.bookTitle
]).join(' ')); ]).join(' '));
this.pageLineCount = 30;
const parsed = this.book.parsed; const parsed = this.book.parsed;
parsed.p = 30;//px, отступ параграфа parsed.p = 30;//px, отступ параграфа
parsed.w = 300;//px, ширина страницы parsed.w = 300;//px, ширина страницы
parsed.measureText = (text, style) => { parsed.measureText = (text, style) => {// eslint-disable-line no-unused-vars
if (style == 'bold') return this.context.measureText(text).width;
return text.length*12;
else
return text.length*3;
}; };
this.parsed = parsed; this.parsed = parsed;
@@ -96,15 +110,24 @@ class TextPage extends Vue {
return; return;
//пустой канвас //пустой канвас
this.items = []; const canvas = this.canvas;
const context = this.context;
context.font = `${this.fontStyle} ${this.fontSize}px ${this.fontName}`;
context.fillStyle = this.backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
if (!this.book) if (!this.book)
return; return;
context.fillStyle = this.textColor;
const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1); const lines = this.parsed.getLines(this.bookPos, this.pageLineCount + 1);
let newItems = [];
let len = lines.length; let len = lines.length;
len = (len > this.pageLineCount ? len = this.pageLineCount : len); len = (len > this.pageLineCount ? len = this.pageLineCount : len);
let y = 0;
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
const line = lines[i]; const line = lines[i];
/* line: /* line:
@@ -117,19 +140,20 @@ class TextPage extends Vue {
} }
}*/ }*/
const item = {text: '', id: line.begin}; let text = '';
for (const part of line.parts) { for (const part of line.parts) {
item.text += part.text; text += part.text;
} }
newItems.push(item);
y += this.lineHeight;
context.fillText(text, 0, y);
} }
this.items = newItems;
this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1)); this.linesUp = this.parsed.getLines(this.bookPos, -(this.pageLineCount + 1));
this.linesDown = lines; this.linesDown = lines;
} }
pageDown() { doPageDown() {
if (this.linesDown) { if (this.linesDown) {
let i = this.pageLineCount; let i = this.pageLineCount;
i--; i--;
@@ -139,7 +163,7 @@ class TextPage extends Vue {
} }
} }
pageUp() { doPageUp() {
if (this.linesUp) { if (this.linesUp) {
let i = this.pageLineCount; let i = this.pageLineCount;
i--; i--;
@@ -150,14 +174,31 @@ class TextPage extends Vue {
} }
} }
doHome() {
this.bookPos = 0;
}
doEnd() {
if (this.parsed.para.length) {
const lastPara = this.parsed.para[this.parsed.para.length - 1];
this.bookPos = lastPara.offset + lastPara.length - 1;
}
}
keyHook(event) { keyHook(event) {
if (event.type == 'keydown') { if (event.type == 'keydown') {
switch (event.key) { switch (event.key) {
case 'PageDown': case 'PageDown':
this.pageDown(); this.doPageDown();
break; break;
case 'PageUp': case 'PageUp':
this.pageUp(); this.doPageUp();
break;
case 'Home':
this.doHome();
break;
case 'End':
this.doEnd();
break; break;
} }
} }
@@ -172,6 +213,9 @@ class TextPage extends Vue {
flex-direction: column; flex-direction: column;
} }
.canvas {
}
pre { pre {
margin: 0; margin: 0;
padding: 0; padding: 0;