私はそれに対する解決策を見つけることを望んでいる問題を抱えています。
Kinetic.js を使用して、特別なルック アンド フィールの HMI ソリューションを作成しています。そのため、ステージ用に 3 つのレイヤーを作成する関数を作成しました。グリッドを含む背景レイヤー、HMI 画面の基本レイアウト用の静的形状を含むレイヤー、およびすべてのインタラクティブ要素 (ボタン、値表示など) 用の 3 番目のレイヤーです。の上...)。ここで、グリッドと静的レイヤーをキャッシュしてパフォーマンスを向上させたいと考えています。このレイヤーは、HMI 画面全体が変更されるまで変更されないためです...
テストとして、次のコードを使用してグリッド レイヤーのキャッシュのコーディングを開始しました。
// Create a grid layer if showGrid is set to TRUE...
console.log('Start to create background grid if required');
if (this.actualPageConfig.showGrid) {
var grid = new Kinetic.Layer();
for (var x=1; x <= this.cols; x++) {
var eLoc = LCARSElements.posToRealPos(x, 1, this.cols, this.rows);
if (x <= this.actualPageConfig.columns) {
grid.add(new Kinetic.Line({
points: [eLoc.x, eLoc.y, eLoc.x, eLoc.height],
stroke: "red",
strokeWidth: 1,
lineCap: "round",
lineJoin: "round"
}));
}
}
for (var y=1; y <= this.rows; y++) {
var eLoc = LCARSElements.posToRealPos(1, y, this.cols, this.rows);
if (y <= this.actualPageConfig.rows) {
grid.add(new Kinetic.Line({
points: [eLoc.x, eLoc.y, eLoc.width, eLoc.y],
stroke: "red",
strokeWidth: 1,
lineCap: "round",
lineJoin: "round"
}));
}
}
// Add grid layer to stage
//this.stage.add(grid); <-- to be replaced by cache image
// Convert grid into an image and add this to the stage
console.log('convert grid to image to increase performance');
grid.toImage({
width: displayManager.stage.getWidth(),
height: displayManager.stage.getHeight(),
callback: function(img) {
var cacheGrid = new Kinetic.Image({
image: img,
x: 0,
y: 0,
width: displayManager.stage.getWidth(),
height: displayManager.stage.getHeight()
});
console.log('insert grid-image to stage');
displayManager.stage.add(cacheGrid);
console.log('redraw stage...');
displayManager.stage.draw();
}
});
}
私の問題は、それが機能していないことです。グリッドが表示されなくなり、コンソール ログに次のエラー情報が表示されます。
Type error: layer.canvas is undefined
layer.canvas.setSize(this.attrs.width, this.attrs.height); kinetic.js (Zeile 3167)
コード「displayManger.stage.add(cacheGrid)が実行されるときにエラーが発生することはすでにわかっています(displayManagerは、このコードが切り取られた外部クラスです)。どこで間違いを犯したか誰にもわかりますか?直接追加するとレイヤーグリッドは何でもうまくいきます...
問題を示すためにjsfiddleを作成しました:jsfiddle
フィドルでは、1 つのパラメーターを変更することで両方のバージョンを実行できます。お役に立てれば....
手伝ってくれてありがとう。
よろしくトルステン