react-native - 如何使用反应原生画布?
问题描述
我想在本机反应中包含画布。我已经安装了https://https://www.npmjs.com/package/react-native-canvas并且它可以工作。
我想做这样的事情:
const canvasElem = document.getElementById("canvasElem");
const ctx = canvasElem.getContext('2d');
const clearBtn = document.getElementById('clearBtn');
ctx.strokeStyle = '#000000';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.lineWidth = 3;
let isDrawing = false;
let lastX = 0;
let lastY = 0;
function clearCanvas() {
ctx.clearRect(0, 0, canvasElem.width, canvasElem.height);
}
function drawSignature(event) {
if(!isDrawing) return;
ctx.beginPath();
//start from
ctx.moveTo(lastX, lastY);
//move to
ctx.lineTo(event.offsetX, event.offsetY) //value from event
ctx.stroke();
[lastX, lastY] = [event.offsetX, event.offsetY];
};
canvasElem.addEventListener('mousemove', drawSignature)
canvasElem.addEventListener('mousedown', (event) => {
isDrawing = true;
[lastX, lastY] = [event.offsetX, event.offsetY];
});
canvasElem.addEventListener('mouseup', () => isDrawing = false);
canvasElem.addEventListener('mouseout', () => isDrawing = false);
clearBtn.addEventListener('click', clearCanvas);
它在浏览器中工作。如何在 react native 中获取 event.offSetX 和 event offSetY 以及事件的等价物(mouseup、mousedown、mousemove 等)?
解决方案
推荐阅读
- apache-flink - 由于一些空闲时段,活动时间没有提前
- node.js - 测试 netlify 函数和 Gatsbyjs 2 时的 ECONNRESET
- r - 如何使用 R 和 JSON 从 Etherscan API 保留换行符?
- ms-access - Ms Access Table Design - 列表或新表
- php - 出现错误:无法将类 mysqli 的对象转换为字符串
- rename - 从文件名中间删除零
- webusb - WEBUSB获取串口数据PL2303
- c# - ASP.NET Core app only works as root
- python-3.x - 使用朴素贝叶斯分类器 Python 获得相同的输出进行文本分类
- python - 为什么我的简单 pytorch 网络不能在 GPU 设备上运行?