|
@@ -1,15 +1,22 @@
|
|
|
<template>
|
|
|
- <div ref="main" class="main" @click.capture="onMouseClick">
|
|
|
- <canvas :style="canvasStyle1" ref="canvas1" class="canvas" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
|
|
|
+ <div ref="main" class="main">
|
|
|
+ <div v-show="activeCanvas" class="layout">
|
|
|
+ <div v-html="page1"></div>
|
|
|
+ </div>
|
|
|
+ <div v-show="!activeCanvas" class="layout">
|
|
|
+ <div v-html="page2"></div>
|
|
|
+ </div>
|
|
|
+ <div v-show="showStatusBar" ref="statusBar" class="layout">
|
|
|
+ <div v-html="statusBar"></div>
|
|
|
+ </div>
|
|
|
+ <div ref="layoutEvents" class="layout events" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
|
|
|
@wheel.prevent.stop="onMouseWheel"
|
|
|
@touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchcancel.prevent.stop="onTouchCancel"
|
|
|
oncontextmenu="return false;">
|
|
|
- </canvas>
|
|
|
- <canvas :style="canvasStyle2" ref="canvas2" class="canvas" @mousedown.prevent.stop="onMouseDown" @mouseup.prevent.stop="onMouseUp"
|
|
|
- @wheel.prevent.stop="onMouseWheel"
|
|
|
- @touchstart.stop="onTouchStart" @touchend.stop="onTouchEnd" @touchcancel.prevent.stop="onTouchCancel"
|
|
|
- oncontextmenu="return false;">
|
|
|
- </canvas>
|
|
|
+ <div v-show="showStatusBar" v-html="statusBarClickable" @mousedown.prevent.stop @touchstart.stop
|
|
|
+ @click.prevent.stop="onStatusBarClick"></div>
|
|
|
+ </div>
|
|
|
+ <canvas ref="offscreenCanvas" style="display: none"></canvas>
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
@@ -33,6 +40,11 @@ export default @Component({
|
|
|
})
|
|
|
class TextPage extends Vue {
|
|
|
activeCanvas = false;
|
|
|
+ showStatusBar = false;
|
|
|
+ page1 = null;
|
|
|
+ page2 = null;
|
|
|
+ statusBar = null;
|
|
|
+ statusBarClickable = null;
|
|
|
|
|
|
lastBook = null;
|
|
|
bookPos = 0;
|
|
@@ -63,13 +75,16 @@ class TextPage extends Vue {
|
|
|
this.prepareNextPage();
|
|
|
}, 100);
|
|
|
|
|
|
+ this.debouncedDrawStatusBar = _.throttle(() => {
|
|
|
+ this.drawStatusBar();
|
|
|
+ }, 60);
|
|
|
+
|
|
|
this.$root.$on('resize', () => {this.$nextTick(this.onResize)});
|
|
|
this.mobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent);
|
|
|
}
|
|
|
|
|
|
mounted() {
|
|
|
- this.canvas1 = this.$refs.canvas1;
|
|
|
- this.canvas2 = this.$refs.canvas2;
|
|
|
+ this.context = this.$refs.offscreenCanvas.getContext('2d');
|
|
|
}
|
|
|
|
|
|
hex2rgba(hex, alpha = 1) {
|
|
@@ -78,36 +93,12 @@ class TextPage extends Vue {
|
|
|
}
|
|
|
|
|
|
async calcDrawProps() {
|
|
|
- this.context1 = this.canvas1.getContext('2d');
|
|
|
- this.context2 = this.canvas2.getContext('2d');
|
|
|
-
|
|
|
this.realWidth = this.$refs.main.clientWidth;
|
|
|
this.realHeight = this.$refs.main.clientHeight;
|
|
|
|
|
|
- let ratio = window.devicePixelRatio;
|
|
|
- if (ratio) {
|
|
|
- this.canvas1.width = this.realWidth*ratio;
|
|
|
- this.canvas1.height = this.realHeight*ratio;
|
|
|
- this.canvas1.style.width = this.$refs.main.clientWidth + 'px';
|
|
|
- this.canvas1.style.height = this.$refs.main.clientHeight + 'px';
|
|
|
- this.context1.scale(ratio, ratio);
|
|
|
-
|
|
|
- this.canvas2.width = this.realWidth*ratio;
|
|
|
- this.canvas2.height = this.realHeight*ratio;
|
|
|
- this.canvas2.style.width = this.$refs.main.clientWidth + 'px';
|
|
|
- this.canvas2.style.height = this.$refs.main.clientHeight + 'px';
|
|
|
- this.context2.scale(ratio, ratio);
|
|
|
- } else {
|
|
|
- this.canvas1.width = this.realWidth;
|
|
|
- this.canvas1.height = this.realHeight;
|
|
|
- this.canvas2.width = this.realWidth;
|
|
|
- this.canvas2.height = this.realHeight;
|
|
|
- }
|
|
|
+ this.$refs.layoutEvents.style.width = this.realWidth + 'px';
|
|
|
+ this.$refs.layoutEvents.style.height = this.realHeight + 'px';
|
|
|
|
|
|
- this.context1.textAlign = 'left';
|
|
|
- this.context2.textAlign = 'left';
|
|
|
- this.context1.textBaseline = 'bottom';
|
|
|
- this.context2.textBaseline = 'bottom';
|
|
|
this.activeCanvas = false;
|
|
|
|
|
|
this.w = this.realWidth - 2*this.indent;
|
|
@@ -120,13 +111,13 @@ class TextPage extends Vue {
|
|
|
this.parsed.w = this.w;// px, ширина текста
|
|
|
this.parsed.font = this.font;
|
|
|
this.parsed.wordWrap = this.wordWrap;
|
|
|
- this.parsed.context = this.context1;
|
|
|
- this.parsed.fontByStyle = this.fontByStyle;
|
|
|
+ this.parsed.measureText = this.measureText;
|
|
|
}
|
|
|
|
|
|
this.statusBarColor = this.hex2rgba(this.textColor, this.statusBarColorAlpha);
|
|
|
this.currentTransition = '';
|
|
|
this.pageChangeDirectionDown = true;
|
|
|
+ this.fontShift = (this.fontShifts[this.fontName] ? this.fontShifts[this.fontName] : 0)/100;
|
|
|
|
|
|
//drawHelper
|
|
|
this.drawHelper.realWidth = this.realWidth;
|
|
@@ -135,6 +126,24 @@ class TextPage extends Vue {
|
|
|
this.drawHelper.backgroundColor = this.backgroundColor;
|
|
|
this.drawHelper.statusBarColor = this.statusBarColor;
|
|
|
this.drawHelper.fontName = this.fontName;
|
|
|
+ this.drawHelper.fontShift = this.fontShift;
|
|
|
+ this.drawHelper.measureText = this.measureText;
|
|
|
+ this.drawHelper.measureTextFont = this.measureTextFont;
|
|
|
+
|
|
|
+ this.$refs.statusBar.style.left = '0px';
|
|
|
+ this.$refs.statusBar.style.top = (this.statusBarTop ? 1 : this.realHeight - this.statusBarHeight) + 'px';
|
|
|
+
|
|
|
+ this.statusBarClickable = this.drawHelper.statusBarClickable(this.statusBarTop, this.statusBarHeight);
|
|
|
+ }
|
|
|
+
|
|
|
+ measureText(text, style) {// eslint-disable-line no-unused-vars
|
|
|
+ this.context.font = this.fontByStyle(style);
|
|
|
+ return this.context.measureText(text).width;
|
|
|
+ }
|
|
|
+
|
|
|
+ measureTextFont(text, font) {// eslint-disable-line no-unused-vars
|
|
|
+ this.context.font = font;
|
|
|
+ return this.context.measureText(text).width;
|
|
|
}
|
|
|
|
|
|
async loadFonts() {
|
|
@@ -157,15 +166,28 @@ class TextPage extends Vue {
|
|
|
this.linesDown = null;
|
|
|
|
|
|
//preloaded fonts
|
|
|
- this.fontList = ['12px ReaderDefault', '12px Arial', '12px ComicSansMS', '12px OpenSans', '12px Roboto', '12px ArialNarrow',
|
|
|
- '12px Georgia', '12px Tahoma', '12px Helvetica', '12px CenturySchoolbook'];
|
|
|
+ this.fontShifts = {//%
|
|
|
+ ReaderDefault: 0,
|
|
|
+ Arial: 5,
|
|
|
+ ComicSansMS: -12,
|
|
|
+ OpenSans: 0,
|
|
|
+ Roboto: 0,
|
|
|
+ ArialNarrow: 0,
|
|
|
+ Georgia: 0,
|
|
|
+ Tahoma: 0,
|
|
|
+ Helvetica: 0,
|
|
|
+ CenturySchoolbook: 0,
|
|
|
+ }
|
|
|
+ this.fontList = [];
|
|
|
+ for (let fontName in this.fontShifts)
|
|
|
+ this.fontList.push(`12px ${fontName}`);
|
|
|
|
|
|
//default draw props
|
|
|
this.textColor = '#000000';
|
|
|
this.backgroundColor = '#478355';
|
|
|
this.fontStyle = '';// 'bold','italic'
|
|
|
this.fontSize = 35;// px
|
|
|
- this.fontName = 'Arial';
|
|
|
+ this.fontName = 'ComicSansMS';
|
|
|
this.lineInterval = 7;// px, межстрочный интервал
|
|
|
this.textAlignJustify = true;// выравнивание по ширине
|
|
|
this.p = 50;// px, отступ параграфа
|
|
@@ -231,22 +253,6 @@ class TextPage extends Vue {
|
|
|
return `${style.italic ? 'italic' : ''} ${style.bold ? 'bold' : ''} ${this.fontSize}px ${this.fontName}`;
|
|
|
}
|
|
|
|
|
|
- get context() {
|
|
|
- return (this.activeCanvas ? this.context1 : this.context2);
|
|
|
- }
|
|
|
-
|
|
|
- get canvas() {
|
|
|
- return (this.activeCanvas ? this.canvas1 : this.canvas2);
|
|
|
- }
|
|
|
-
|
|
|
- get canvasStyle1() {
|
|
|
- return (this.activeCanvas ? {'z-index': 11} : {'z-index': 10});
|
|
|
- }
|
|
|
-
|
|
|
- get canvasStyle2() {
|
|
|
- return (this.activeCanvas ? {'z-index': 10} : {'z-index': 11});
|
|
|
- }
|
|
|
-
|
|
|
draw(immediate) {
|
|
|
if (this.book && this.bookPos > 0 && this.bookPos >= this.parsed.textLength) {
|
|
|
this.doEnd();
|
|
@@ -254,10 +260,12 @@ class TextPage extends Vue {
|
|
|
}
|
|
|
|
|
|
this.activeCanvas = !this.activeCanvas;
|
|
|
- const context = this.context;
|
|
|
|
|
|
- if (immediate) {
|
|
|
- this.drawPage(context, this.bookPos);
|
|
|
+ if (immediate) {
|
|
|
+ if (this.activeCanvas)
|
|
|
+ this.page1 = this.drawPage(this.bookPos);
|
|
|
+ else
|
|
|
+ this.page2 = this.drawPage(this.bookPos);
|
|
|
} else {
|
|
|
if (this.pageChangeDirectionDown && this.pagePrepared && this.bookPos == this.bookPosPrepared) {
|
|
|
this.linesDown = this.linesDownNext;
|
|
@@ -265,7 +273,10 @@ class TextPage extends Vue {
|
|
|
this.pagePrepared = false;
|
|
|
this.debouncedPrepareNextPage();
|
|
|
} else {
|
|
|
- this.drawPage(context, this.bookPos);
|
|
|
+ if (this.activeCanvas)
|
|
|
+ this.page1 = this.drawPage(this.bookPos);
|
|
|
+ else
|
|
|
+ this.page2 = this.drawPage(this.bookPos);
|
|
|
this.pagePrepared = false;
|
|
|
this.debouncedPrepareNextPage();
|
|
|
}
|
|
@@ -282,21 +293,23 @@ class TextPage extends Vue {
|
|
|
this.currentTransition = '';
|
|
|
this.pageChangeDirectionDown = false;//true только если PgDown
|
|
|
}
|
|
|
+
|
|
|
+ this.debouncedDrawStatusBar();
|
|
|
}
|
|
|
|
|
|
- drawPage(context, bookPos, nextChangeLines) {
|
|
|
+ drawPage(bookPos, nextChangeLines) {
|
|
|
if (!this.lastBook)
|
|
|
return;
|
|
|
|
|
|
- context.fillStyle = this.backgroundColor;
|
|
|
- context.fillRect(0, 0, this.realWidth, this.realHeight);
|
|
|
+ let out = `<div class="layout" style="width: ${this.realWidth}px; height: ${this.realHeight}px;` +
|
|
|
+ ` color: ${this.textColor}; background-color: ${this.backgroundColor}">`;
|
|
|
|
|
|
- if (!this.book || !this.parsed.textLength)
|
|
|
- return;
|
|
|
+ if (!this.book || !this.parsed.textLength) {
|
|
|
+ out += '</div>';
|
|
|
+ return out;
|
|
|
+ }
|
|
|
|
|
|
- context.font = this.font;
|
|
|
- context.fillStyle = this.textColor;
|
|
|
- const spaceWidth = context.measureText(' ').width;
|
|
|
+ const spaceWidth = this.measureText(' ', {});
|
|
|
|
|
|
const lines = this.parsed.getLines(bookPos, 2*this.pageLineCount);
|
|
|
if (!nextChangeLines) {
|
|
@@ -307,7 +320,7 @@ class TextPage extends Vue {
|
|
|
this.linesUpNext = this.parsed.getLines(bookPos, -2*this.pageLineCount);
|
|
|
}
|
|
|
|
|
|
- let y = -this.lineInterval/2 + (this.h - this.pageLineCount*this.lineHeight)/2;
|
|
|
+ let y = -this.lineInterval/2 + (this.h - this.pageLineCount*this.lineHeight)/2 + this.fontSize*this.fontShift;
|
|
|
if (this.showStatusBar)
|
|
|
y += this.statusBarHeight*(this.statusBarTop ? 1 : 0);
|
|
|
|
|
@@ -328,13 +341,15 @@ class TextPage extends Vue {
|
|
|
}*/
|
|
|
|
|
|
let indent = this.indent + (line.first ? this.p : 0);
|
|
|
- y += this.lineHeight;
|
|
|
|
|
|
let lineText = '';
|
|
|
let center = false;
|
|
|
+ let centerStyle = {};
|
|
|
for (const part of line.parts) {
|
|
|
lineText += part.text;
|
|
|
center = center || part.style.center;
|
|
|
+ if (part.style.center)
|
|
|
+ centerStyle = part.style.center;
|
|
|
}
|
|
|
|
|
|
let filled = false;
|
|
@@ -349,13 +364,13 @@ class TextPage extends Vue {
|
|
|
|
|
|
let x = indent;
|
|
|
for (const part of line.parts) {
|
|
|
- context.font = this.fontByStyle(part.style);
|
|
|
+ const font = this.fontByStyle(part.style);
|
|
|
let partWords = part.text.split(' ');
|
|
|
|
|
|
for (let i = 0; i < partWords.length; i++) {
|
|
|
let word = partWords[i];
|
|
|
- context.fillText(word, x, y);
|
|
|
- x += context.measureText(word).width + (i < partWords.length - 1 ? space : 0);
|
|
|
+ out += this.drawHelper.fillText(word, x, y, font);
|
|
|
+ x += this.measureText(word, part.style) + (i < partWords.length - 1 ? space : 0);
|
|
|
}
|
|
|
}
|
|
|
filled = true;
|
|
@@ -365,31 +380,31 @@ class TextPage extends Vue {
|
|
|
// просто выводим текст
|
|
|
if (!filled) {
|
|
|
let x = indent;
|
|
|
- x = (center ? this.indent + (this.w - context.measureText(lineText).width)/2 : x);
|
|
|
+ x = (center ? this.indent + (this.w - this.measureText(lineText, centerStyle))/2 : x);
|
|
|
for (const part of line.parts) {
|
|
|
let text = part.text;
|
|
|
- context.font = this.fontByStyle(part.style);
|
|
|
- context.fillText(text, x, y);
|
|
|
- x += context.measureText(text).width;
|
|
|
+ const font = this.fontByStyle(part.style);
|
|
|
+ out += this.drawHelper.fillText(text, x, y, font);
|
|
|
+ x += this.measureText(text, part.style);
|
|
|
}
|
|
|
}
|
|
|
+ y += this.lineHeight;
|
|
|
}
|
|
|
|
|
|
- this.drawStatusBar(context, lines);
|
|
|
+ out += '</div>';
|
|
|
+ return out;
|
|
|
}
|
|
|
|
|
|
- drawStatusBar(context, lines) {
|
|
|
- if (!lines)
|
|
|
- lines = this.linesDown;
|
|
|
-
|
|
|
- if (this.showStatusBar) {
|
|
|
+ drawStatusBar() {
|
|
|
+ if (this.showStatusBar && this.linesDown) {
|
|
|
+ const lines = this.linesDown;
|
|
|
let i = this.pageLineCount;
|
|
|
if (this.keepLastToFirst)
|
|
|
i--;
|
|
|
i = (i > lines.length - 1 ? lines.length - 1 : i);
|
|
|
|
|
|
- this.drawHelper.drawStatusBar(context, this.statusBarTop, this.statusBarHeight,
|
|
|
- this.statusBarColor, lines[i].end, this.parsed.textLength, this.title);
|
|
|
+ this.statusBar = this.drawHelper.drawStatusBar(this.statusBarTop, this.statusBarHeight,
|
|
|
+ lines[i].end, this.parsed.textLength, this.title);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -399,7 +414,7 @@ class TextPage extends Vue {
|
|
|
await sleep(60*1000);
|
|
|
|
|
|
if (this.book && this.parsed.textLength) {
|
|
|
- this.drawStatusBar(this.context);
|
|
|
+ this.debouncedDrawStatusBar();
|
|
|
}
|
|
|
this.timeRefreshing = false;
|
|
|
this.refreshTime();
|
|
@@ -427,8 +442,10 @@ class TextPage extends Vue {
|
|
|
if (i >= 0 && this.linesDown.length > i) {
|
|
|
this.bookPosPrepared = this.linesDown[i].begin;
|
|
|
|
|
|
- const ctx = (!this.activeCanvas ? this.context1 : this.context2);
|
|
|
- this.drawPage(ctx, this.bookPosPrepared, true);
|
|
|
+ if (this.activeCanvas)
|
|
|
+ this.page2 = this.drawPage(this.bookPosPrepared, true);//наоборот
|
|
|
+ else
|
|
|
+ this.page1 = this.drawPage(this.bookPosPrepared, true);
|
|
|
|
|
|
this.pagePrepared = true;
|
|
|
}
|
|
@@ -606,30 +623,8 @@ class TextPage extends Vue {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- checkPointInStatusBar(pointX, pointY) {
|
|
|
- let titleBar = {x1: 0, y1: 0, x2: this.realWidth/2, y2: this.statusBarHeight + 1};
|
|
|
- if (!this.statusBarTop) {
|
|
|
- titleBar.y1 += this.realHeight - this.statusBarHeight + 1;
|
|
|
- titleBar.y2 += this.realHeight - this.statusBarHeight + 1;
|
|
|
- }
|
|
|
-
|
|
|
- if (pointX >= titleBar.x1 && pointX <= titleBar.x2 &&
|
|
|
- pointY >= titleBar.y1 && pointY <= titleBar.y2) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- onMouseClick(event) {
|
|
|
- if (this.showStatusBar && this.book) {
|
|
|
- const pointX = event.pageX - this.canvas.offsetLeft;
|
|
|
- const pointY = event.pageY - this.canvas.offsetTop;
|
|
|
-
|
|
|
- if (this.checkPointInStatusBar(pointX, pointY)) {
|
|
|
- window.open(this.meta.url, '_blank');
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
+ onStatusBarClick() {
|
|
|
+ window.open(this.meta.url, '_blank');
|
|
|
}
|
|
|
|
|
|
handleClick(pointX, pointY) {
|
|
@@ -639,12 +634,6 @@ class TextPage extends Vue {
|
|
|
100: {30: 'PgUp', 100: 'PgDown'}
|
|
|
};
|
|
|
|
|
|
- if (this.showStatusBar && this.book) {
|
|
|
- if (this.checkPointInStatusBar(pointX, pointY)) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
const w = pointX/this.realWidth*100;
|
|
|
const h = pointY/this.realHeight*100;
|
|
|
|
|
@@ -695,9 +684,15 @@ class TextPage extends Vue {
|
|
|
position: relative;
|
|
|
}
|
|
|
|
|
|
-.canvas {
|
|
|
+.layout {
|
|
|
margin: 0;
|
|
|
padding: 0;
|
|
|
position: absolute;
|
|
|
+ z-index: 10;
|
|
|
+}
|
|
|
+
|
|
|
+.events {
|
|
|
+ z-index: 20;
|
|
|
+ background-color: rgba(0,0,0,0);
|
|
|
}
|
|
|
</style>
|