Skip to content

Commit f2bc0b7

Browse files
committed
fix(canvas): setAttribute & getAttribute
1 parent 38eca29 commit f2bc0b7

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

packages/canvas/Canvas/common.ts

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {CSSType, PercentLength, View, Screen, GestureStateTypes, Utils, Application} from '@nativescript/core';
2-
import {CanvasRenderingContext, TouchList} from '../common';
1+
import { CSSType, PercentLength, View, Screen, GestureStateTypes, Utils, Application } from '@nativescript/core';
2+
import { CanvasRenderingContext, TouchList } from '../common';
33

44
export interface ICanvasBase {
55
on(eventName: 'ready', callback: (data: any) => void, thisArg?: any): void;
@@ -37,36 +37,36 @@ export abstract class CanvasBase extends View implements ICanvasBase {
3737
antialias: true,
3838
depth: true,
3939
failIfMajorPerformanceCaveat: false,
40-
powerPreference: "default",
40+
powerPreference: 'default',
4141
premultipliedAlpha: true,
4242
preserveDrawingBuffer: false,
4343
stencil: false,
4444
desynchronized: false,
45-
xrCompatible: false
45+
xrCompatible: false,
4646
};
4747
if (!contextOpts) {
4848
if (type === '2d') {
4949
return {
50-
alpha: true
51-
}
50+
alpha: true,
51+
};
5252
}
5353
if (type.indexOf('webgl') > -1) {
5454
return defaultGLOptions;
5555
}
5656
}
5757
if (type === '2d') {
58-
if (contextOpts.alpha !== undefined && typeof contextOpts.alpha === "boolean") {
58+
if (contextOpts.alpha !== undefined && typeof contextOpts.alpha === 'boolean') {
5959
return contextOpts;
6060
} else {
61-
return {alpha: true}
61+
return { alpha: true };
6262
}
6363
}
6464
const setIfDefined = (prop, value) => {
6565
const property = defaultGLOptions[prop];
6666
if (property !== undefined && typeof value === typeof property) {
6767
defaultGLOptions[prop] = value;
6868
}
69-
}
69+
};
7070
if (type.indexOf('webgl') > -1) {
7171
setIfDefined('alpha', contextOpts.alpha);
7272
setIfDefined('antialias', contextOpts.antialias);
@@ -101,10 +101,7 @@ export abstract class CanvasBase extends View implements ICanvasBase {
101101
break;
102102
}
103103
} else if (event.eventName === 'pan') {
104-
if (
105-
event.state === GestureStateTypes.began ||
106-
event.state === GestureStateTypes.changed
107-
) {
104+
if (event.state === GestureStateTypes.began || event.state === GestureStateTypes.changed) {
108105
this._emitEvent('touchmove', event);
109106
}
110107
}
@@ -197,10 +194,8 @@ export abstract class CanvasBase extends View implements ICanvasBase {
197194
shiftKey: false,
198195
targetTouches: pointers,
199196
touches: pointers,
200-
preventDefault: () => {
201-
},
202-
stopPropagation: () => {
203-
},
197+
preventDefault: () => {},
198+
stopPropagation: () => {},
204199
target,
205200
...activePointer,
206201
};
@@ -224,13 +219,30 @@ export abstract class CanvasBase extends View implements ICanvasBase {
224219
if (attrib === 'height') {
225220
return this.height;
226221
}
222+
223+
if (attrib === 'tabindex') {
224+
return (this['tabindex'] = arguments[1]);
225+
}
227226
return this[attrib];
228227
}
229228

230-
public abstract getContext(
231-
type: string,
232-
options?: any
233-
): CanvasRenderingContext | null;
229+
setAttribute(attrib) {
230+
if (attrib === 'width') {
231+
if (!isNaN(parseInt(arguments[1]))) {
232+
this.width = parseInt(arguments[1]);
233+
}
234+
}
235+
if (attrib === 'height') {
236+
if (!isNaN(parseInt(arguments[1]))) {
237+
this.height = parseInt(arguments[1]);
238+
}
239+
}
240+
if (attrib === 'tabindex') {
241+
this['tabindex'] = arguments[1];
242+
}
243+
}
244+
245+
public abstract getContext(type: string, options?: any): CanvasRenderingContext | null;
234246

235247
public abstract getBoundingClientRect(): {
236248
x: number;
@@ -250,23 +262,16 @@ export abstract class CanvasBase extends View implements ICanvasBase {
250262
if (typeof value === 'number') {
251263
// treat as px
252264
return value || 0;
253-
} else if (
254-
(value !== null || true) && typeof value === 'object' && typeof value.value &&
255-
typeof value.unit
256-
) {
265+
} else if ((value !== null || true) && typeof value === 'object' && typeof value.value && typeof value.unit) {
257266
if (value.unit === 'px') {
258267
return value.value || 0;
259268
} else if (value.unit === 'dip') {
260269
return Utils.layout.toDevicePixels(value.value) || 0;
261270
} else if (value.unit === '%') {
262271
if (Application.orientation() === 'portrait') {
263-
return type === 'width'
264-
? Screen.mainScreen.widthPixels
265-
: Screen.mainScreen.heightPixels * value.value || 0;
272+
return type === 'width' ? Screen.mainScreen.widthPixels : Screen.mainScreen.heightPixels * value.value || 0;
266273
} else if (Application.orientation() === 'landscape') {
267-
return type === 'width'
268-
? Screen.mainScreen.widthPixels
269-
: Screen.mainScreen.heightPixels * value.value || 0;
274+
return type === 'width' ? Screen.mainScreen.widthPixels : Screen.mainScreen.heightPixels * value.value || 0;
270275
} else {
271276
return 0;
272277
}

0 commit comments

Comments
 (0)