Comments (3)
Thank you very much its working fine using (await measureText('xxx')).width
from react-native-canvas.
Please try (await measureText('xxx')).width
from react-native-canvas.
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)
- cropping an image HOT 1
- Not able to render image on ios 15.2.1 HOT 3
- Path2D differs from the Canvas API, in that it's not connected to a Canvas element HOT 2
- getcontext of a canvas gives TypeError: canvas.getContext is not a function HOT 2
- Difference in drawImage coordinates and getImageData/putImageData coordinates HOT 2
- why Why did I use getImageData() for more than 1 minute? HOT 2
- Canvas remain black on windows
- How do you get fillRect to occupy full dimensions? HOT 4
- ImageData/getImageData/putImageData strange behaviour HOT 2
- error HOT 2
- New architecture support for fabric HOT 1
- require cycle node_modules\react-native-canvas HOT 1
- canvas.loadFromJSON missing in react-native-canvas
- Error: Unable to resolve module ./CanvasRenderingContext2D HOT 1
- Not able to add fontFamily with font
- Application gets hang after 10 mins due to drawing on canvas continuously
- Can't draw image greater than 2048x2048 HOT 1
- Virtual/In-Memory Canvas HOT 1
- Can't draw Image on release HOT 1
- Doesn't work at all with Expo HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-canvas.