8
8
import { ButtonDefaults } from '../constants' ;
9
9
import { trackMousePosition , isMouseInside } from '../utils'
10
10
import type { CanvasComponentContext } from '../types'
11
+
11
12
//TODO adjust Opacity when focus, Blur
12
13
type PressEvent = { || } ;
13
14
type ButtonProps = { |
@@ -67,18 +68,21 @@ type ButtonProps = {|
67
68
| } ;
68
69
69
70
function renderButton ( props : ButtonProps , apeContext :CanvasComponentContext , parentLayout ) {
70
- const { spatialGeometry } = parentLayout ;
71
71
const { ctx} = apeContext ;
72
72
73
73
// If is relative and x and y haven't be processed, don't render
74
- if ( ! spatialGeometry ) return null ;
75
74
// start drawing the canvas
75
+ console . log ( '[PROPS]' , props )
76
76
const { title, color} = props ;
77
+ if ( ! title ) {
78
+ throw Error ( "Title required!" )
79
+ }
77
80
const borderRadius = ButtonDefaults . containerStyle . borderRadius ;
78
81
const backgroundColor = ButtonDefaults . containerStyle . backgroundColor ;
79
- let x = spatialGeometry . x || 0 ;
80
- let y = spatialGeometry . y || 0 ;
81
- let width = x + y * title . length / 3 ;
82
+ let x = 40 ;
83
+ let y = 300 ;
84
+ const textWidth = ctx . measureText ( title ) . width ;
85
+ let width = textWidth * 1.5 ;
82
86
let height = ButtonDefaults . containerStyle . height ;
83
87
let globalStyle = {
84
88
width : width ,
@@ -91,20 +95,19 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
91
95
} ;
92
96
const resetStyle = newStyle => {
93
97
globalStyle = { ...globalStyle , newStyle} ;
94
- console . log ( 'style)))))' , globalStyle ) ;
98
+
95
99
} ;
96
100
const redrawButton = ctx => {
97
101
// TODO reset Style on focus
98
102
let newStyle = {
99
103
lineWidth : 2 ,
100
104
borderColor : '#ccc' ,
101
105
} ;
102
- //let prevStyle = globalStyle
103
106
resetStyle ( newStyle ) ;
104
107
} ;
105
108
106
109
ctx . beginPath ( ) ;
107
- ctx . fillStyle = backgroundColor ;
110
+ ctx . fillStyle = color || ButtonDefaults . containerStyle . backgroundColor
108
111
ctx . moveTo ( x , y ) ;
109
112
/**
110
113
* Top Right Radius
@@ -137,10 +140,16 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
137
140
ctx . strokeStyle = globalStyle . borderColor ;
138
141
ctx . stroke ( ) ;
139
142
ctx . fillStyle = ButtonDefaults . textStyle . color ;
140
- ctx . font = `${ ButtonDefaults . textStyle . fontSize } Helvetica` ;
141
- //ctx.fillText('Start', x+ width/2 , y + height / 2);
142
- ctx . textAlign = 'center' ;
143
- ctx . fillText ( title , x + width / 2 , y + height / 2 ) ;
143
+
144
+ //set the fontSize
145
+ const fontArgs = ctx . font . split ( ' ' ) ;
146
+ const newSize = ` ${ ButtonDefaults . textStyle . fontSize } px` ;
147
+ ctx . font = newSize + ' ' + fontArgs [ fontArgs . length - 1 ] ;
148
+
149
+ // ctx.textAlign = 'center';
150
+
151
+ // ctx.fillText(title, 400 / 2, y + height / 2,textWidth);
152
+ ctx . fillText ( title , ( x + textWidth / 2.5 ) , y + height / 2 ) ;
144
153
ctx . closePath ( ) ;
145
154
146
155
const onClick = ( event : SyntheticMouseEvent < HTMLButtonElement > ) => {
@@ -179,6 +188,8 @@ Note onClick will need to share scope with this function to work properly.
179
188
180
189
}
181
190
191
+
192
+
182
193
export default function createButtonInstance ( props : ButtonProps ) : mixed {
183
194
return {
184
195
type : 'Button' ,
0 commit comments