【问题标题】:Cannot read property "getContext" of undefined - electron无法读取未定义的属性“getContext” - 电子
【发布时间】:2019-04-06 19:20:09
【问题描述】:

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

我遇到的问题是 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.

谢谢!

【问题讨论】:

    标签: javascript electron easeljs electron-forge preloadjs


    【解决方案1】:

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

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

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

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

    canvasEl = document.getElementById("stage");
    

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

    希望对您有所帮助。

    【讨论】:

    • 嗨,兰尼,谢谢你的回答,这很有意义,但我必须抓住画布才能真正动态地改变宽度和高度。我找到了原因,因为它们在我的 HD 上而不是在 localhost 上提供,所以基本上没有找到图像的预加载器。我使用 live-server 解决了它,并在创建窗口之前从主进程启动它。还是谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多