|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 |
|
3 | | -var React = require('react'); |
4 | | -var PropTypes = require('prop-types'); |
5 | | -var createReactClass = require('create-react-class'); |
| 3 | +var React = require("react"); |
| 4 | +var PropTypes = require("prop-types"); |
| 5 | +var createReactClass = require("create-react-class"); |
6 | 6 |
|
7 | | -var { |
8 | | - View, |
9 | | - Platform, |
10 | | -} = require('react-native'); |
11 | | -var { WebView } = require('react-native-webview'); |
| 7 | +var { View, Platform } = require("react-native"); |
| 8 | +var { WebView } = require("react-native-webview"); |
12 | 9 |
|
13 | 10 | function getRenderHTML(context, render, generator) { |
14 | | - return ` |
| 11 | + return ` |
15 | 12 | <style> |
16 | | - *{margin:0;padding:0;}canvas{transform:translateZ(0);} |
| 13 | + *{margin:0;padding:0;overflow:hidden;}canvas{transform:translateZ(0);} |
17 | 14 | </style> |
18 | 15 | <canvas></canvas> |
19 | 16 | <script> |
20 | 17 | var canvas = document.querySelector('canvas'); |
21 | 18 | (${render}).call(${context}, canvas); |
22 | 19 | (${generator}).call('', ${context} , canvas); |
23 | 20 | </script>" |
24 | | - `; |
| 21 | + `; |
25 | 22 | } |
26 | 23 |
|
27 | 24 | function generateImage(context, canvas) { |
28 | | - if (window.postMessage.length !== 1) { |
29 | | - setTimeout(function() { |
30 | | - (generateImage).call(this, context, canvas); |
31 | | - }, 100); |
32 | | - } else { |
33 | | - const image = document.createElement('canvas'); |
34 | | - const imageContext = image.getContext('2d'); |
35 | | - image.width = context.size; |
36 | | - image.height = context.size; |
37 | | - imageContext.fillStyle = context.bgColor; |
38 | | - imageContext.fillRect(0, 0, context.size, context.size); |
39 | | - imageContext.drawImage(canvas, 0, 0); |
40 | | - window.postMessage(image.toDataURL()); |
41 | | - } |
| 25 | + if (window.postMessage.length !== 1) { |
| 26 | + setTimeout(function() { |
| 27 | + generateImage.call(this, context, canvas); |
| 28 | + }, 100); |
| 29 | + } else { |
| 30 | + const image = document.createElement("canvas"); |
| 31 | + const imageContext = image.getContext("2d"); |
| 32 | + image.width = context.size; |
| 33 | + image.height = context.size; |
| 34 | + imageContext.fillStyle = context.bgColor; |
| 35 | + imageContext.fillRect(0, 0, context.size, context.size); |
| 36 | + imageContext.drawImage(canvas, 0, 0); |
| 37 | + window.postMessage(image.toDataURL()); |
| 38 | + } |
42 | 39 | } |
43 | 40 |
|
44 | 41 | var Canvas = createReactClass({ |
45 | | - propTypes: { |
46 | | - style: PropTypes.object, |
47 | | - context: PropTypes.object, |
48 | | - render: PropTypes.func.isRequired, |
49 | | - onLoad: PropTypes.func, |
50 | | - onLoadEnd: PropTypes.func, |
51 | | - }, |
52 | | - |
53 | | - getImage: function(event) { |
54 | | - if(this.props.generateImage) { |
55 | | - this.props.generateImage(event.nativeEvent.data); |
56 | | - } |
57 | | - }, |
| 42 | + propTypes: { |
| 43 | + style: PropTypes.object, |
| 44 | + context: PropTypes.object, |
| 45 | + render: PropTypes.func.isRequired, |
| 46 | + onLoad: PropTypes.func, |
| 47 | + onLoadEnd: PropTypes.func |
| 48 | + }, |
58 | 49 |
|
59 | | - render() { |
60 | | - var contextString = JSON.stringify(this.props.context); |
61 | | - var renderString = this.props.render.toString(); |
62 | | - var generate = generateImage.toString(); |
63 | | - return ( |
64 | | - <View style={this.props.style}> |
65 | | - <WebView |
66 | | - automaticallyAdjustContentInsets={false} |
67 | | - scalesPageToFit={Platform.OS === 'android'} |
68 | | - contentInset={{top: 0, right: 0, bottom: 0, left: 0}} |
69 | | - source={{ html: getRenderHTML(contextString, renderString, generate), baseUrl: '' }} |
70 | | - opaque={false} |
71 | | - underlayColor={'transparent'} |
72 | | - style={this.props.style} |
73 | | - javaScriptEnabled={true} |
74 | | - scrollEnabled={false} |
75 | | - onLoad={this.props.onLoad} |
76 | | - onLoadEnd={this.props.onLoadEnd} |
77 | | - originWhitelist={['*']} |
78 | | - onMessage={this.getImage} |
79 | | - /> |
80 | | - </View> |
81 | | - ); |
| 50 | + getImage: function(event) { |
| 51 | + if (this.props.generateImage) { |
| 52 | + this.props.generateImage(event.nativeEvent.data); |
82 | 53 | } |
| 54 | + }, |
| 55 | + |
| 56 | + render() { |
| 57 | + var contextString = JSON.stringify(this.props.context); |
| 58 | + var renderString = this.props.render.toString(); |
| 59 | + var generate = generateImage.toString(); |
| 60 | + return ( |
| 61 | + <View style={this.props.style}> |
| 62 | + <WebView |
| 63 | + automaticallyAdjustContentInsets={false} |
| 64 | + scalesPageToFit={false} |
| 65 | + contentInset={{ top: 0, right: 0, bottom: 0, left: 0 }} |
| 66 | + source={{ |
| 67 | + html: getRenderHTML(contextString, renderString, generate), |
| 68 | + baseUrl: "" |
| 69 | + }} |
| 70 | + opaque={false} |
| 71 | + underlayColor={"transparent"} |
| 72 | + overScrollMode="never" |
| 73 | + style={this.props.style} |
| 74 | + javaScriptEnabled={true} |
| 75 | + scrollEnabled={false} |
| 76 | + onLoad={this.props.onLoad} |
| 77 | + onLoadEnd={this.props.onLoadEnd} |
| 78 | + originWhitelist={["*"]} |
| 79 | + onMessage={this.getImage} |
| 80 | + showsVerticalScrollIndicator={false} |
| 81 | + showsHorizontalScrollIndicator={false} |
| 82 | + /> |
| 83 | + </View> |
| 84 | + ); |
| 85 | + } |
83 | 86 | }); |
84 | 87 |
|
85 | 88 | module.exports = Canvas; |
0 commit comments