88import { ButtonDefaults } from '../constants' ;
99import { trackMousePosition , isMouseInside } from '../utils'
1010import type { CanvasComponentContext } from '../types'
11+
1112//TODO adjust Opacity when focus, Blur
1213type PressEvent = { || } ;
1314type ButtonProps = { |
@@ -67,18 +68,21 @@ type ButtonProps = {|
6768| } ;
6869
6970function renderButton ( props : ButtonProps , apeContext :CanvasComponentContext , parentLayout ) {
70- const { spatialGeometry } = parentLayout ;
7171 const { ctx} = apeContext ;
7272
7373 // If is relative and x and y haven't be processed, don't render
74- if ( ! spatialGeometry ) return null ;
7574 // start drawing the canvas
75+ console . log ( '[PROPS]' , props )
7676 const { title, color} = props ;
77+ if ( ! title ) {
78+ throw Error ( "Title required!" )
79+ }
7780 const borderRadius = ButtonDefaults . containerStyle . borderRadius ;
7881 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 ;
8286 let height = ButtonDefaults . containerStyle . height ;
8387 let globalStyle = {
8488 width : width ,
@@ -91,20 +95,19 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
9195 } ;
9296 const resetStyle = newStyle => {
9397 globalStyle = { ...globalStyle , newStyle} ;
94- console . log ( 'style)))))' , globalStyle ) ;
98+
9599 } ;
96100 const redrawButton = ctx => {
97101 // TODO reset Style on focus
98102 let newStyle = {
99103 lineWidth : 2 ,
100104 borderColor : '#ccc' ,
101105 } ;
102- //let prevStyle = globalStyle
103106 resetStyle ( newStyle ) ;
104107 } ;
105108
106109 ctx . beginPath ( ) ;
107- ctx . fillStyle = backgroundColor ;
110+ ctx . fillStyle = color || ButtonDefaults . containerStyle . backgroundColor
108111 ctx . moveTo ( x , y ) ;
109112 /**
110113* Top Right Radius
@@ -137,10 +140,16 @@ function renderButton(props: ButtonProps, apeContext:CanvasComponentContext, par
137140 ctx . strokeStyle = globalStyle . borderColor ;
138141 ctx . stroke ( ) ;
139142 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 ) ;
144153 ctx . closePath ( ) ;
145154
146155 const onClick = ( event : SyntheticMouseEvent < HTMLButtonElement > ) => {
@@ -179,6 +188,8 @@ Note onClick will need to share scope with this function to work properly.
179188
180189}
181190
191+
192+
182193export default function createButtonInstance ( props : ButtonProps ) : mixed {
183194 return {
184195 type : 'Button' ,
0 commit comments