Use React Native draw Canvas(ios)

前端之家收集整理的这篇文章主要介绍了Use React Native draw Canvas(ios)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Use React Native draw Canvas(ios)


1.Canvas.js
  1. 'use strict'
  2. var React = require('react-native');
  3. var {
  4. View,WebView
  5. } = React;
  6.  
  7. var Canvas = React.createClass({
  8. propTypes: {
  9. context: React.PropTypes.object,render: React.PropTypes.func.isrequired
  10. },render() {
  11. var contextString = JSON.stringify(this.props.context);
  12. var renderString = this.props.render.toString();
  13. return (
  14. <View>
  15. <WebView automaticallyAdjustContentInsets={false}
  16. html={"<style>*{margin:0;padding:0;}canvas{position:absolute;transform:translateZ(0);}</style><canvas></canvas><script>var canvas=document.querySelector('canvas');(" + renderString + ").call(" + contextString + ",canvas);</script>"}
  17. opaque={false} underlayColor={'transparent'}
  18. style={this.props.style} />
  19. </View>
  20. );
  21. }
  22. });
  23. module.exports = Canvas;

2. index.ios.js
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. */
  5. 'use strict';
  6. var React = require('react-native');
  7. var {
  8. AppRegistry,Text,View,Component
  9. } = React;
  10. var Canvas = require('./Canvas');
  11.  
  12. var SimpleApp = React.createClass({
  13. render() {
  14. var context = {msg: "Hello!"};
  15. return (
  16. <View style={styles.container}>
  17. <View>
  18. <Canvas context={context}
  19. render={renderCanvas}
  20. style={{height: 200,width: 200}} />
  21. </View>
  22. </View>
  23. )
  24. }
  25. });
  26.  
  27. function renderCanvas(canvas) {
  28. var message = this.msg;
  29. var ctx = canvas.getContext('2d'),particles = [],patriclesNum = 50,w = 200,h = 200,colors = ['#f35d4f','#f36849','#c0d988','#6ddaf1','#f1e85b'];
  30. canvas.width = 200;
  31. canvas.height = 200;
  32. canvas.style.left = (window.innerWidth - 200) / 2 + 'px';
  33. if (window.innerHeight > 200)
  34. canvas.style.top = (window.innerHeight - 200) / 2 + 'px';
  35.  
  36. function draw() {
  37. ctx.font = "20px Georgia";
  38. ctx.fillText(message,10,50);
  39. }
  40. draw();
  41. }
  42.  
  43. var styles = React.StyleSheet.create({
  44. container: {
  45. padding: 30,marginTop: 65,borderColor: '#e7e7e7',},});
  46.  
  47. AppRegistry.registerComponent('SimpleApp',() => SimpleApp);

猜你在找的React相关文章