0

在触发从 ipcMain 到窗口的事件后,我试图在easeljs 中动态生成精灵表。

我遇到的问题是 preloadjs/easeljs 在控制台中触发了这个错误:

无法读取未定义的属性“getContext”

它只发生在我打包应用程序之后,而不是在开发模式下,一切都按预期工作。

这是我从窗口中获取的代码

import { ipcRenderer } from "electron";
let canvasEl, stage, queue;

ipcRenderer.on("render-spritesheets", (_, data) => {
  const files = data.previewFiles;
  const info = data.info;

  canvasEl = document.getElementById("stage");
  canvasEl.width = info.width;
  canvasEl.height = info.height;
  canvasEl.style.backgroundColor = "black";

  stage = new createjs.Stage("stage");

  createjs.Ticker.timingMode = createjs.Ticker.RAF_SYNCHED;
  createjs.Ticker.framerate = info.framerate;

  function handleTick() {
    stage.update();
  }
  createjs.Ticker.addEventListener("tick", handleTick);

  function onError(err) {
    console.log(err);
  }

  function onComplete(event) {
    const spritesheets = files
      .map(f => f.path)
      .map((_, i) => queue.getResult(`spr_${i}`));
    console.log(spritesheets);
    const spritesheetData = {
      images: spritesheets,
      frames: {
        width: +info.width,
        height: +info.height,
        count: +info.frames,
        regX: 0,
        regY: 0
      }
    };

    const spriteSheet = new createjs.SpriteSheet(spritesheetData);
    const animation = new createjs.Sprite(spriteSheet);
    stage.addChild(animation);
    animation.play();
  }

  function loadSpritesheets() {
    const manifest = [];
    files.forEach((file, index) => {
      manifest.push({ src: file.path, id: `spr_${index}` });
    });

    queue = new createjs.LoadQueue(false);
    queue.addEventListener("complete", onComplete);
    queue.addEventListener("error", onError);
    queue.loadManifest(manifest, true);
  }

  loadSpritesheets();
});

这里是我的 electron.js 代码。

import { app, BrowserWindow, ipcMain } from 'electron';
import installExtension, { REACT_DEVELOPER_TOOLS } from 'electron-devtools-installer';
import { enableLiveReload } from 'electron-compile';

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let previewWindow;

const isDevMode = process.execPath.match(/[\\/]electron/);

if (isDevMode) enableLiveReload({ strategy: 'react-hmr' });

const createWindow = async () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 400,
    height: 650,
    minWidth: 400,
    minHeight: 650,
    maxHeight: 650,
    maxWidth: 400,
    frame: false
  });

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/index.html`);

  // Open the DevTools.
  if (isDevMode) {
    await installExtension(REACT_DEVELOPER_TOOLS);
    mainWindow.webContents.openDevTools();
  }

  // Emitted when the window is closed.
  mainWindow.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null;
  });

  ipcMain.on('close-app', (evt, arg) => {
    mainWindow = null;
    previewWindow = null;
    app.quit();
  })

  ipcMain.on("open-preview", (evt, data) => {

    const mainWinPos = mainWindow.getPosition();

    const width = data.info.width;
    const height = data.info.height + 56;

    previewWindow = new BrowserWindow({
      width: width,
      height: height,
      minWidth: width,
      minHeight: height,
      maxHeight: height,
      maxWidth: width,
      frame: false,
      parent: mainWindow
    });

    previewWindow.setPosition(mainWinPos[0] - (width + 20), mainWinPos[1], false);

    previewWindow.loadURL(`file://${__dirname}/preview.html`);

    previewWindow.webContents.once('did-finish-load', () => {
      previewWindow.webContents.openDevTools();
      previewWindow.webContents.send('render-spritesheets', data);
    });

    previewWindow.on("closed", () => previewWindow = null);
    ipcMain.on("close-preview", () => {
      if (previewWindow) previewWindow.close();
    });


  });


};

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit();
  }
});

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow();
  }
});

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.

谢谢!

4

1 回答 1

0

这个问题几乎总是由 canvas 元素尚未在 DOM 中引起的。当您使用:

 stage = new createjs.Stage("stage");

舞台将使用document.getElementById("stage");. 如果它还没有在 DOM 中,那么您对上下文的任何访问都将失败。

这条线有用吗?它做同样的事情。

canvasEl = document.getElementById("stage");

通常框架会创建一个文档片段,而您必须查询它来获取您的阶段参考。

希望有帮助。

于 2019-04-07T20:05:08.727 回答