岄. 2024-05-11 13:21 采纳率: 91.7%
浏览 574

报错TypeError: canvas.getContext is not a function,如何解决?


<template>
    <view>
        <div class="inner">
            <video ref="video" :src="videoSrc" type="video/mp4" id="myVideo" width="200px" height="200px"
                show-snapshot-button></video>
            <button @click="play">播放</button>
            <button @click="pause">暂停</button>
            <button @click="downloadVideo">下载</button>
            <button id="save-btn" @click="saveBtn">保存图片</button>
            <canvas id="can" style="width: 200px;height: 200px;"></canvas>
        </div>
        <n-bar></n-bar>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                mp: null,
                playStatus: 1,
                autoPlay: false,
                videoSrc: '../../static/1.mp4',
                shot: '',
                videoNa: '',
                videoBur: true
            }
        },
        onLoad(option) {
            this.videoSrc = this.$baseUrlVideo + '/' + option.acNa
            this.videoNa = option.acNa
            if (uni.getSystemInfoSync().platform === 'android') {
                // this.videoBur = true
            }
        },
        mounted() {

        },
        computed: {
            videoHtml: function() {
                return `<video  autoplay loop muted controls="controls" width="100%" height="870px" src="${this.videoSrc}"></video>`;
            }
        },
        methods: {
            downloadVideo() {
                const videoUrl = this.$refs.video.src;
                const a = document.createElement("a");
                a.href = videoUrl;
                a.download = `${this.videoNa}.mp4`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            },
            play() {
                this.$refs.video.play();
            },
            pause() {
                this.$refs.video.pause();
            },
            saveBtn() {
                const video = document.querySelector('#myVideo');
                const canvas = document.getElementById('can');
                canvas.width = video.videoWidth;
                canvas.height = video.videoHeight;
                console.log(canvas);
                const ctx = canvas.getContext('2d');
                ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
                canvas.toBlob(blob => {
                    const url = URL.createObjectURL(blob);
                    const a = document.createElement('a');
                    a.href = url;
                    a.download = 'my-image.png';
                    document.body.appendChild(a);
                    a.click();
                    document.body.removeChild(a);
                    URL.revokeObjectURL(url);
                }, 'image/png');
            }
        }
    }
</script>
<style>
    canvas {
        width: 400rpx;
        height: 400rpx;
    }

    .inner {
        text-align: center;
    }
</style>

报错TypeError: canvas.getContext is not a function,具体代码修改

  • 写回答

3条回答 默认 最新

  • 专家-皆知 2024-05-11 13:21
    关注

    请检查canvas元素是否正确插入到DOM中。

    以上回复有部分引自免费微信小程序:皆我百晓生。 如问题尚未解决,还请稍待,如有疑问请回复进一步沟通。

    评论

报告相同问题?

问题事件

  • 创建了问题 5月11日

悬赏问题

  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译
  • ¥100 对反编译和ai熟悉的开发者。
  • ¥15 带序列特征的多输出预测模型
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?