前回の続き。
2つの Image.onload コールバックを検知してから描写しないといけない。
それなら最初のコールバックの中で次の onload を呼び出しすればいいじゃない。
それに気が付くまで二週間も掛かった、いや連休前で仕事も忙しかったし。
これでインラインのように処理、、、、、できなかった。
コールバックの中でしか画像の大きさを得ることができないのが痛い。
画像が横長なのか調べる必要があるけど2つ目のコールバックまで何もできない。
これを得ないと現在表示しているページ番号が不定になってしまう。
Linux, macOS 版 Comipoli のコードはやっぱり流用できそうにない。
しかたがないので prev 処理は左右ページを入れ替えるという最終手段に。
var LocaleComicViewer = {
readComic(files) {
//
// etc...
//
// image
this.firstPage = new Image();
this.secondPage = new Image();
this.firstPage.onload = ()=> {
if (this.num == -1) {
this.spread = false;
this.num = 0;
this.drawPage();
} else {
if (this.prev) {
if (this.firstPage.width > this.firstPage.height) {
this.spread = false;
this.drawPage();
} else {
if (this.num > 0) {
this.spread = true;
this.secondPage.src = this.comics[this.num - 1];
this.num -= 1;
} else {
this.spread = false;
this.drawPage();
}
}
} else {
if (this.firstPage.width > this.firstPage.height) {
this.spread = false;
this.drawPage();
} else {
if (this.comics.length > this.num + 1) {
this.spread = true;
this.secondPage.src = this.comics[this.num + 1];
} else {
this.spread = false;
this.drawPage();
}
}
}
}
}
this.secondPage.onload = ()=> {
if (this.secondPage.width > this.secondPage.height) {
this.spread = false;
this.drawPage();
} else {
this.drawPage();
}
}
this.firstPage.src = this.comics[0];
},
drawPage() {
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
let aw = this.canvas.width;
let ah = this.canvas.height;
let w = this.firstPage.width;
let h = this.firstPage.height;
if (w - h > 0 || !this.spread) {
//
// etc...
//
} else {
if (this.LtoR || this.prev) {
//
// etc...
//
full src
cv,js
なんとかなったけどまだ動作があやしい。
Linux 版 Comipoli も安定させるのに一年くらい使ったし、まあこんなもんや。
ところで Firefox は location.reload だけでは読み直ししないのね。
javascript – window.location.reload not working for Firefox and Chrome – Stack Overflow
setTimeout(()=> {location.reload(true);});
で解決したけど何故に。。。。。
それと今回から class をヤメて ES6 簡略構文を使うことにした。
gjs の Lang.Class とほぼ同様だしアロー関数で this は保持できるし。
new でインスタンスを作る必要が無いのであればこっちのほうが簡単なのでお勧め。