vue3——用canvas展示图片做出直播效果

需求:后台会通过WebSocket每10毫秒返回一张base64的图片,如果直接使用el-image的话,图片切换的时候会出现不停的闪烁和卡顿问题,所以用canvas来解决这个问题

废话不多说,直接上例子组件

子组件

<template>
  <canvas ref="canvas" :width="width" :height="height"></canvas>
</template>
  
<script setup>
import { ref, onMounted, onUnmounted, watch } from "vue";

// 声明组件 Props
const props = defineProps({
  base64Images: String, // 图片字符串
  fps: Number, // 帧率
  width: { type: Number, default: 400 }, // 画面宽度
  height: { type: Number, default: 300 }, // 画面高度
});

// 使用 ref 创建响应式数据
const canvas = ref(null);
let currentFrame = 0;
let animationInterval = null;

// 在组件挂载后开始动画
onMounted(() => {
  startAnimation();
});

// 在组件卸载前清除定时器
onUnmounted(() => {
  stopAnimation();
});

// 监听 base64Images 变化,更新动画
watch(
  () => props.base64Images,
  (newValue, oldValue) => {
    if (newValue !== oldValue) {
      stopAnimation(); // 停止之前的动画
      currentFrame = 0; // 重置当前帧索引
      startAnimation(); // 开始新的动画
    }
  }
);

// 开始动画
const startAnimation = () => {
  const frameDuration = 1000 / props.fps;
  animationInterval = setInterval(renderFrame, frameDuration);
};

// 渲染每一帧
const renderFrame = () => {
  const img = new Image();
  img.onload = () => {
    const ctx = canvas.value.getContext("2d");
    ctx.clearRect(0, 0, canvas.value.width, canvas.value.height);
    ctx.drawImage(img, 0, 0, props.width, props.height);
  };
  img.src = props.base64Images;
};

// 停止动画
const stopAnimation = () => {
  clearInterval(animationInterval);
};
</script>
  

父组件使用

<canvasVideo :base64Images="cameraContinuous" :fps="25" :width="400" :height="400" />
import canvasVideo from './components/canvasVideo.vue'
//cameraContinuous 需要通过websocket不停获取后台推送图片
const cameraContinuous = ref('')

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 中,可以使用以下步骤利用 Canvas 裁剪图片并保存: 1. 在 Vue3 中,可以使用 `ref` 或 `setup()` 中的 `refs` 来获取 `<canvas>` 元素的引用。 ```html <template> <div> <canvas ref="canvas"></canvas> <input type="file" accept="image/*" @change="onFileChange"> <button @click="cropImage">Crop Image</button> </div> </template> ``` ```javascript import { ref } from 'vue'; export default { setup() { const canvasRef = ref(null); return { canvasRef, }; }, }; ``` 2. 在 `onFileChange` 方法中获取上传的图片,并将其绘制到 `<canvas>` 中。 ```javascript async function onFileChange(event) { const file = event.target.files[0]; const image = new Image(); image.onload = () => { canvasRef.value.width = image.width; canvasRef.value.height = image.height; const context = canvasRef.value.getContext('2d'); context.drawImage(image, 0, 0); }; image.src = URL.createObjectURL(file); } ``` 3. 在 `cropImage` 方法中获取裁剪后的图片,并将其保存。 ```javascript async function cropImage() { const context = canvasRef.value.getContext('2d'); const imageData = context.getImageData(0, 0, canvasRef.value.width, canvasRef.value.height); const data = imageData.data; // 获取裁剪后的图片数据 const croppedImageData = context.getImageData(x, y, width, height); const croppedData = croppedImageData.data; // 将裁剪后的图片绘制到新的 Canvas 中 const newCanvas = document.createElement('canvas'); newCanvas.width = width; newCanvas.height = height; const newContext = newCanvas.getContext('2d'); newContext.putImageData(croppedImageData, 0, 0); // 将新的 Canvas 转换为图片,并保存 const newImage = new Image(); newImage.src = newCanvas.toDataURL('image/png'); document.body.appendChild(newImage); } ``` 在以上代码中,`x`、`y`、`width`、`height` 分别表示裁剪区域的左上角坐标和宽高。裁剪后的图片数据可以使用 `getImageData()` 方法获取,然后再将其绘制到新的 Canvas 中,最后将新的 Canvas 转换为图片,并保存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值