canvas_27 保存画布为图片

canvas_27 保存画布为图片

<template>
    <view class="zcvs">

        <view class="zcvs-item">
            <view>保存画布为图片</view>
            <view>
                <canvas canvas-id="cvs" id="cvs" style="width: 400px; height: 400px;border: 1px solid #007AFF;" />
            </view>
            <image :src="imgSrc" mode="aspectFill"></image>
        </view>

    </view>
</template>

<script>
    export default {
        data() {
            return {
                imgSrc: "",
            };
        },
        onReady() {
            this.drawCvs();
        },
        methods: {
            drawCvs() {
                const ctx = uni.createCanvasContext('cvs');
                ctx.setFillStyle("#007AFF");
                ctx.fillRect(0, 0, 400, 200);
                ctx.draw();

                uni.canvasToTempFilePath({
                    x: 0,
                    y: 0,
                    width: 400,
                    height: 200,
                    destWidth: 100,
                    destHeight: 100,
                    canvasId: 'cvs',
                    success: res => {
                        this.imgSrc = res.tempFilePath;
                        this.$forceUpdate();
                    }
                })
            },
        }
    }
</script>

<style lang="scss" scoped>

</style>

 

上一篇:李正元400题 -- 卷四


下一篇:c#的if判断与异常处理