我想在视频中绘制(使用 html5 canvas)
I want to draw (using html5 canvas) inside video in react
我的简单 React 应用程序有两个功能,一个播放视频源的视频标签,以及一个用 html5 canvas 制作的钢笔工具。我希望能够使用该钢笔工具在我的视频中绘制。
我一直在谷歌上搜索一些答案,但没有找到明确的方法。
看看我的代码
function App(props) {
const canvasRef = useRef(null);
const contextRef = useRef(null);
const [isDrawing, setIsDrawing] = useState(false);
useEffect(() => {
const canvas = canvasRef.current;
canvas.width = window.innerWidth * 2;
canvas.height = window.innerHeight * 2;
canvas.style.width = `${window.innerWidth}px`;
canvas.style.height = `${window.innerHeight}px`;
const context = canvas.getContext('2d');
context.scale(2, 2);
context.lineCap = 'round';
context.strokeStyle = 'black';
context.lineWidth = 5;
contextRef.current = context;
}, []);
const startDrawing = ({ nativeEvent }) => {
const { offsetX, offsetY } = nativeEvent;
contextRef.current.beginPath();
contextRef.current.moveTo(offsetX, offsetY);
setIsDrawing(true);
};
const finishDrawing = () => {
contextRef.current.closePath();
setIsDrawing(false);
};
const draw = ({ nativeEvent }) => {
if (!isDrawing) {
return;
}
const { offsetX, offsetY } = nativeEvent;
contextRef.current.lineTo(offsetX, offsetY);
contextRef.current.stroke();
};
// return <canvas onMouseDown={startDrawing} onMouseUp={finishDrawing} onMouseMove={draw} ref={canvasRef} />;
return (
<div>
<canvas onMouseDown={startDrawing} onMouseUp={finishDrawing} onMouseMove={draw} ref={canvasRef} />
<video id="v" controls loop width="500">
<source src={video} type="video/mp4" />
</video>
</div>
);
}
有可能。
首先,您需要像这样对 canvas 应用一些样式:
position: absolute;z-index: 999;
.
然后您可以像这样向视频应用其他样式:
z-index: 11;
如果是这样,canvas 和视频将重叠,并且 canvas 将像这样放在视频之上:
在这里,您会遇到一个问题...例如,在某些情况下您可能想要控制视频。意思是,您想 pause/play/stop
随时观看视频。
为此,您还需要为视频放置自定义暂停、播放和停止按钮。但是那些按钮的 z-index 应该高于 999,因为 canvas 的 z-index 是 999。
谢谢
使用绝对位置和相对容器
https://codesandbox.io/s/elated-sun-36eno?file=/src/styles.css
我的简单 React 应用程序有两个功能,一个播放视频源的视频标签,以及一个用 html5 canvas 制作的钢笔工具。我希望能够使用该钢笔工具在我的视频中绘制。 我一直在谷歌上搜索一些答案,但没有找到明确的方法。
看看我的代码
function App(props) {
const canvasRef = useRef(null);
const contextRef = useRef(null);
const [isDrawing, setIsDrawing] = useState(false);
useEffect(() => {
const canvas = canvasRef.current;
canvas.width = window.innerWidth * 2;
canvas.height = window.innerHeight * 2;
canvas.style.width = `${window.innerWidth}px`;
canvas.style.height = `${window.innerHeight}px`;
const context = canvas.getContext('2d');
context.scale(2, 2);
context.lineCap = 'round';
context.strokeStyle = 'black';
context.lineWidth = 5;
contextRef.current = context;
}, []);
const startDrawing = ({ nativeEvent }) => {
const { offsetX, offsetY } = nativeEvent;
contextRef.current.beginPath();
contextRef.current.moveTo(offsetX, offsetY);
setIsDrawing(true);
};
const finishDrawing = () => {
contextRef.current.closePath();
setIsDrawing(false);
};
const draw = ({ nativeEvent }) => {
if (!isDrawing) {
return;
}
const { offsetX, offsetY } = nativeEvent;
contextRef.current.lineTo(offsetX, offsetY);
contextRef.current.stroke();
};
// return <canvas onMouseDown={startDrawing} onMouseUp={finishDrawing} onMouseMove={draw} ref={canvasRef} />;
return (
<div>
<canvas onMouseDown={startDrawing} onMouseUp={finishDrawing} onMouseMove={draw} ref={canvasRef} />
<video id="v" controls loop width="500">
<source src={video} type="video/mp4" />
</video>
</div>
);
}
有可能。
首先,您需要像这样对 canvas 应用一些样式:
position: absolute;z-index: 999;
.
然后您可以像这样向视频应用其他样式:
z-index: 11;
如果是这样,canvas 和视频将重叠,并且 canvas 将像这样放在视频之上:
在这里,您会遇到一个问题...例如,在某些情况下您可能想要控制视频。意思是,您想 pause/play/stop
随时观看视频。
为此,您还需要为视频放置自定义暂停、播放和停止按钮。但是那些按钮的 z-index 应该高于 999,因为 canvas 的 z-index 是 999。
谢谢
使用绝对位置和相对容器
https://codesandbox.io/s/elated-sun-36eno?file=/src/styles.css