Javascript 在WebView中反应本机画布
我正在尝试用React Native 0.17编写一个“绘制”Android应用程序 我有一个Javascript 在WebView中反应本机画布,javascript,android,canvas,webview,react-native,Javascript,Android,Canvas,Webview,React Native,我正在尝试用React Native 0.17编写一个“绘制”Android应用程序 我有一个Canvas组件(灵感来自: 我的HTML/JS在浏览器(Chrome)中运行良好,但不幸的是,我无法在我的React原生应用程序中绘制任何内容 有人能帮我吗? 我应该在React Native中收听哪些事件 当您在标记中注释一些JS行时,ES6模板字符串似乎中断 尝试删除它们: export default `<html> <head></head> <
Canvas
组件(灵感来自:
我的HTML/JS在浏览器(Chrome)中运行良好,但不幸的是,我无法在我的React原生应用程序中绘制任何内容
有人能帮我吗?
我应该在React Native中收听哪些事件 当您在
标记中注释一些JS行时,ES6模板字符串似乎中断
尝试删除它们:
export default `<html>
<head></head>
<body>
<h1 id='toto'>TATA</h1>
<br />
<div id="sketch">
<canvas id="paint" style='border: 1px solid black'></canvas>
</div>
<script>
(function() {
var canvas = document.querySelector('#paint');
var ctx = canvas.getContext('2d');
var sketch = document.querySelector('#sketch');
var sketch_style = getComputedStyle(sketch);
canvas.width = parseInt(sketch_style.getPropertyValue('width'));
canvas.height = parseInt(sketch_style.getPropertyValue('height'));
var mouse = {x: 0, y: 0};
var start_events = ["mousedown", "touchstart"];
var move_events = ["mousemove", "touchmove"];
var end_events = ["mouseup", "touchend"];
move_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
}, false);
});
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.strokeStyle = 'back';
start_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
ctx.beginPath();
ctx.moveTo(mouse.x, mouse.y);
move_events.forEach(function(me){ canvas.addEventListener(me, onPaint, false); });
}, false);
});
end_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
move_events.forEach(function(me){ canvas.removeEventListener(me, onPaint, false); });
}, false);
});
var onPaint = function() {
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
};
}());
</script>
</body>
</html>`;
导出默认值`
塔塔
(功能(){
var canvas=document.querySelector(“#paint”);
var ctx=canvas.getContext('2d');
var sketch=document.querySelector(“#sketch”);
var sketch_style=getComputedStyle(草图);
canvas.width=parseInt(sketch_style.getPropertyValue('width');
canvas.height=parseInt(sketch_style.getPropertyValue('height');
变量鼠标={x:0,y:0};
var start_events=[“mousedown”、“touchstart”];
var move_events=[“mousemove”,“touchmove”];
var end_events=[“mouseup”,“touchend”];
move_events.forEach(函数(事件){
canvas.addEventListener(事件、函数(e){
mouse.x=e.pageX-this.offsetLeft;
mouse.y=e.pageY-this.offsetTop;
},假);
});
ctx.lineWidth=5;
ctx.lineJoin='round';
ctx.lineCap='圆形';
ctx.strokeStyle='back';
启动\u events.forEach(函数(事件){
canvas.addEventListener(事件、函数(e){
ctx.beginPath();
移动到(mouse.x,mouse.y);
move_events.forEach(函数(me){canvas.addEventListener(me,onPaint,false);});
},假);
});
结束事件。forEach(函数(事件){
canvas.addEventListener(事件、函数(e){
move_events.forEach(函数(me){canvas.removeEventListener(me,onPaint,false);});
},假);
});
var onPaint=函数(){
ctx.lineTo(mouse.x,mouse.y);
ctx.stroke();
};
}());
`;
react native canvas
此处的当前维护人员,这里的人想寻找一个反应本地的画布解决方案:我想让你知道<代码>反应本地画布<代码>今天是一个稳定的解决方案,它与以前使用的代码注入黑客有很大的不同。 < P>是基于GPU OpenGL GLSL着色器的C++原生画布组件,在Android和iOS上运行良好。您可以看到,一个易于移植的示例运行在Android、iOS和Web上
export default `<html>
<head></head>
<body>
<h1 id='toto'>TATA</h1>
<br />
<div id="sketch">
<canvas id="paint" style='border: 1px solid black'></canvas>
</div>
<script>
(function() {
var canvas = document.querySelector('#paint');
var ctx = canvas.getContext('2d');
var sketch = document.querySelector('#sketch');
var sketch_style = getComputedStyle(sketch);
canvas.width = parseInt(sketch_style.getPropertyValue('width'));
canvas.height = parseInt(sketch_style.getPropertyValue('height'));
var mouse = {x: 0, y: 0};
var start_events = ["mousedown", "touchstart"];
var move_events = ["mousemove", "touchmove"];
var end_events = ["mouseup", "touchend"];
/* Mouse Capturing Work */
move_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
}, false);
});
/* Drawing on Paint App */
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.strokeStyle = 'back';
start_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
ctx.beginPath();
ctx.moveTo(mouse.x, mouse.y);
move_events.forEach(function(me){ canvas.addEventListener(me, onPaint, false); });
}, false);
});
end_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
move_events.forEach(function(me){ canvas.removeEventListener(me, onPaint, false); });
}, false);
});
var onPaint = function() {
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
};
}());
</script>
</body>
</html>`;
export default `<html>
<head></head>
<body>
<h1 id='toto'>TATA</h1>
<br />
<div id="sketch">
<canvas id="paint" style='border: 1px solid black'></canvas>
</div>
<script>
(function() {
var canvas = document.querySelector('#paint');
var ctx = canvas.getContext('2d');
var sketch = document.querySelector('#sketch');
var sketch_style = getComputedStyle(sketch);
canvas.width = parseInt(sketch_style.getPropertyValue('width'));
canvas.height = parseInt(sketch_style.getPropertyValue('height'));
var mouse = {x: 0, y: 0};
var start_events = ["mousedown", "touchstart"];
var move_events = ["mousemove", "touchmove"];
var end_events = ["mouseup", "touchend"];
move_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
mouse.x = e.pageX - this.offsetLeft;
mouse.y = e.pageY - this.offsetTop;
}, false);
});
ctx.lineWidth = 5;
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
ctx.strokeStyle = 'back';
start_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
ctx.beginPath();
ctx.moveTo(mouse.x, mouse.y);
move_events.forEach(function(me){ canvas.addEventListener(me, onPaint, false); });
}, false);
});
end_events.forEach(function(event) {
canvas.addEventListener(event, function(e) {
move_events.forEach(function(me){ canvas.removeEventListener(me, onPaint, false); });
}, false);
});
var onPaint = function() {
ctx.lineTo(mouse.x, mouse.y);
ctx.stroke();
};
}());
</script>
</body>
</html>`;