javascript - 函数传递给 video.addEventListener ("play") 事件未定义

标签 javascript reactjs canvas html5-canvas

我目前正在开发一个 React 组件,该组件需要将来自视频元素的帧传递到 Canvas 元素上。

当视频播放时, Canvas 上没有绘制任何东西。我相信这是因为我编写的绘制框架的方法不在范围内,如果我对接下来要尝试的方法有任何想法,我将不胜感激。

import React, { Component, Fragment } from 'react'

class Stream extends Component {
  constructor(props) {
    super(props);
    this.video = React.createRef();
    this.canvas = React.createRef();
    this.screen = React.createRef();

    this.drawFrame = this.drawFrame.bind(this);
  }

  drawFrame(video) {
    const screen = this.screen.current
    screen.height = 500
    screen.width = 500
    let context = screen.getContext('2d')
    context.drawImage(video, 0, 0, screen.width, screen.height)
    context.drawImage(video, 0, 0, screen.width, screen.height)
    requestAnimationFrame(this.drawFrame)
    console.log("Screen", screen)
  }

  componentDidMount() {
    // This code runs a video stream.
    const video = this.video.current
    navigator.mediaDevices.getUserMedia({ video: true, audio: false })
    .then(function(stream) {
        video.srcObject = stream
        video.play()
        // Says drawFrame is undefined if line below in uncommented.
        //video.addEventListener("play", this.drawFrame(video), false);
        console.log("Video is playing from component")
    })
    .catch(function(err) {
        console.log("An error occurred! " + err)
    })
  }

  componentDidUpdate() {}

  render() {
    return(
      <Fragment>
          <video
            ref={this.video}
            width="500"
            height="500"
          >Video stream is not available.</video>
          <canvas
            ref={this.screen}
            width="500"
            height="500"
          >Canvas is not available in this browser.</canvas>
          <canvas
            ref={this.canvas}
            width="500"
            height="500"
          >Canvas is not available in this browser.</canvas>
      </Fragment>
    )
  }
}

export default Stream

最佳答案

您丢失了 this 引用的上下文。只需使用 arrow function对于词汇上下文:

.then((stream) => ...

如果你不能使用箭头函数,那么用老方法来做,关闭 this 并将它存储在一个不同的变量中,然后使用它:

componentDidMount() {
    const _this = this;
    // This code runs a video stream.
    const video = this.video.current
    navigator.mediaDevices.getUserMedia({ video: true, audio: false })
    .then(function(stream) {
        video.srcObject = stream
        video.play()
        video.addEventListener("play", _this.drawFrame(video), false);
        console.log("Video is playing from component")
    })

关于javascript - 函数传递给 video.addEventListener ("play") 事件未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448142/

相关文章:

javascript - react native : How to define a javascript class

Javascript SPA 多语言和 LocalStorage

javascript - 仅从外部 url 解析 json Html 和 JS

javascript - 为什么我的 react html 渲染不起作用?

javascript - 如何在 Canvas 中绘制一个形状(多边形)并在其后面放置一个图像。形状应该在顶部

javascript - 即时客户端图像到 webp 转换器......但 png 不透明

java - 滚动时 JScrollPane 不重新绘制 Canvas?

c# - JavaScript 和 Windows 窗体之间的交互

javascript - 如何用javascript锚定?

reactjs - Material UI 组件在 react-frame-component 中丢失样式