video被看做图片处理 new fabric.Image
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> .canvas-container { border: 1px dotted #ccc; display: inline-block; vertical-align: top; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 1200px; height: 700px; position: relative; user-select: none; } #c { border: 1px solid rgb(204, 204, 204); position: absolute; width: 1200px; height: 700px; left: 0px; top: 0px; touch-action: none; user-select: none; } </style> <script src="../fabric.js"></script> </head> <body> <video id="video1" muted height="360" width="480"> <source src="https://cloud.video.taobao.com/play/u/828291614/p/1/e/6/t/1/333154089379.mp4" /> </video> <video id="webcam" height="360" width="500" style="display: none"></video> <div class="canvas-container"> <canvas id="c" width="1200" height="700"></canvas> </div> <script> var canvas = new fabric.Canvas('c'); var video1El = document.getElementById('video1'); var webcamEl = document.getElementById('webcam'); var video1 = new fabric.Image(video1El, { left: 200, top: 300, angle: -15, originX: 'center', originY: 'center', objectCaching: false }); var webcam = new fabric.Image(webcamEl, { left: 539, top: 328, angle: 94.5, originX: 'center', originY: 'center', objectCaching: false }); canvas.add(video1); video1.getElement().play(); //下面是摄像头相关内容 if (navigator.mediaDevices === undefined) { navigator.mediaDevices = {}; } if (navigator.mediaDevices.getUserMedia === undefined) { navigator.mediaDevices.getUserMedia = function(constraints) { var getUserMedia = navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; if (!getUserMedia) { return Promise.reject( new Error('getUserMedia is not implemented in this browser') ); } return new Promise(function(resolve, reject) { getUserMedia.call(navigator, constraints, resolve, reject); }); }; } navigator.mediaDevices .getUserMedia({ video: true }) .then(function getWebcamAllowed(localMediaStream) { webcamEl.srcObject = localMediaStream; canvas.add(webcam); webcam.moveTo(0); webcam.getElement().play(); }) .catch(function getWebcamAllowed(e) {}); fabric.util.requestAnimFrame(function render() { canvas.renderAll(); fabric.util.requestAnimFrame(render); }); </script> </body> </html>