Coder Social home page Coder Social logo

Comments (3)

PatilRupaliAress avatar PatilRupaliAress commented on August 16, 2024 1

Thank you very much its working fine using (await measureText('xxx')).width

from react-native-canvas.

iddan avatar iddan commented on August 16, 2024

Please try (await measureText('xxx')).width

from react-native-canvas.

XiaoLin1995 avatar XiaoLin1995 commented on August 16, 2024

I need to draw a turntable, but there are some problems.
I don't know how to solve it.
can you help me?
Thanks very much.

import React from 'react'
import {
    View,
    StyleSheet
} from  'react-native'
import Canvas from 'react-native-canvas';


export default class Fan extends  React.Component{
  constructor () {
    super()
    this.state = {
      prize: [], 
      outsideRadius: 125,
      insideRadius: 10, 
      textRadius: 100, 
      startAngle: 0, 
      border: 5, 
    }
    this.state.prize = [
      {
        id: 1,
        name: 'thanks',
        value: 0, 
        bgColor: '#ff693b',
        color: '#ffffff', 
        probability: 5 
      },
      {
        id: 2,
        name: '40',
        value: 40,
        bgColor: '#ffce39',
        color: '#fe693c',
        probability: 11
      },
      {
        id: 3,
        name: '20',
        value: 20,
        bgColor: '#ff693b',
        color: '#ffffff',
        probability: 22
      },
      {
        id: 4,
        name: 'thanks',
        value: 0,
        bgColor: '#ffce39',
        color: '#fe693c',
        probability: 5
      },
      {
        id: 5,
        name: '40',
        value: 40,
        bgColor: '#ff693b',
        color: '#ffffff',
        probability: 11
      },
      {
        id: 6,
        name: '20',
        value: 20,
        bgColor: '#ffce39',
        color: '#fe693c',
        probability: 22
      },
      {
        id: 7,
        name: '65',
        value: 65,
        bgColor: '#ff693b',
        color: '#ffffff',
        probability: 2
      },
      {
        id: 8,
        name: '20',
        value: 20,
        bgColor: '#ffce39',
        color: '#fe693c',
        probability: 22
      }
    ] 
  }
  handleCanvas = (canvas) => {
     let arc = Math.PI / (this.state.prize.length / 2)
     let ctx = canvas.getContext('2d')
     ctx.clearRect(0, 0, this.state.outsideRadius * 2, this.state.outsideRadius * 2)
     ctx.strokeStyle = '#ff9800'
     ctx.lineWidth = this.state.border * 2
     ctx.font = '14px Microsoft YaHei'
     for (let i = 0; i < this.state.prize.length; i++) {
       let angle = this.state.startAngle + i * arc
       ctx.fillStyle = this.state.prize[i].bgColor
       ctx.beginPath()
       ctx.arc(this.state.outsideRadius, this.state.outsideRadius, this.state.outsideRadius - this.state.border, angle, angle + arc, false)
       ctx.stroke()
       ctx.arc(this.state.outsideRadius, this.state.outsideRadius, this.state.insideRadius - this.state.border, angle + arc, angle, true)
       ctx.fill()
       ctx.save()
       ctx.fillStyle = this.state.prize[i].color
       let text = this.state.prize[i].name
       let lineHeight = 15
       ctx.translate(this.state.outsideRadius + Math.cos(angle + arc / 2) * this.state.textRadius, this.state.outsideRadius + Math.sin(angle + arc / 2) * this.state.textRadius)
       ctx.rotate(angle + arc / 2 + Math.PI / 2)
       if (text.length > 6) {
         text = text.substring(0, 6) + '||' + text.substring(6)
         let texts = text.split('||')
         for (var j = 0; j < texts.length; j++) {
           ctx.fillText(texts[j], -ctx.measureText(texts[j]).width / 2, (j + 1) * lineHeight)
         }
       } else {
         let textWidth = (ctx.measureText(text)).width
         ctx.fillText(text, -textWidth / 2, lineHeight)
         // this.fillCanvasText(ctx, text, lineHeight)
       }
       ctx.restore()
     }
  }
  async fillCanvasText (ctx, text, lineHeight) {
    let textWidth = (await ctx.measureText(text)).width
    alert(textWidth)
    ctx.fillText(text, -textWidth / 2, lineHeight)
  }
  render(){
    return(
      <View style={styles.container}>
        <Canvas ref={this.handleCanvas} styles={{width: 100, height: 100}}/>
      </View>
    )
  }
}

const styles = StyleSheet.create({
  container: {
   flex: 1,
   justifyContent: 'center',
   alignItems: 'center'
  }
})

from react-native-canvas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.