Based on the JavaScript running environment on virtual machine (VMs), HUAWEI Quick Game provides a series of common web APIs based on JavaScript to adapt web games to quick games. The APIs function the same in both quick games and the web environment.
Global Document Object | Parameter | Description |
|---|---|---|
document | readyState | Document loading status. |
visibilityState | Visibility of the document object. | |
documentElement | Root element of the document object. | |
hidden | Indicates whether to hide a document. The value is of the Boolean type. The options are true (yes) and false (no). | |
location | Location object that contains document URL information. | |
head | Head element in the current document. | |
body | Body element in the current document. | |
scripts | Collection of all script elements in the current document. |
Method | Description |
|---|---|
Creates an element with the specified namespace URI and qualified name. | |
Creates an element of a specified type. | |
Obtains an element of a specified ID. | |
Obtains a collection of HTML elements with all the given tags. | |
Obtains a collection of elements with given tag names. | |
Obtains the first HTML element within the document that matches the specified selector or group of selectors. | |
Obtains a list of elements within the document that match the specified group of selectors. | |
Creates a text element. | |
Creates an event of a specified type. |
Creates an element with the specified namespace URI and qualified name.
Parameter | Type | Mandatory | Description |
|---|---|---|---|
namespaceURI | string | Yes | URI of the namespace associated with the element. |
qualifiedName | string | Yes | Type of the element to be created. |
options | object | No | Optional parameters (not supported). |
document.createElementNS("https://www.w3.org/1999/xhtml", "a"); Parameter | Type | Mandatory | Description |
|---|---|---|---|
tagName | string | Yes | Type of the element to be created. |
this.canvas = document.createElement('canvas'); Quick Game implements standard web local storage. For details, please refer to Local Storage API Reference.
A maximum of 9 data records, each with a size of 1 MB, can be written to the data cache because the data keys and table structure in the data cache also occupy some storage space. The tenth data record will fail to be written because the storage space is exceeded.
Method | Description |
|---|---|
Modifies the content stored in the app database. | |
Reads the content stored in the app database. | |
Deletes the content stored in the app database. | |
Clears the content stored in the app database. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
keyName | string | Yes | Index. |
keyValue | string | Yes | Value. |
function populateStorage() {
localStorage.setItem('bgcolor', 'red');
localStorage.setItem('font', 'Helvetica');
localStorage.setItem('image', 'myCat.png');
} Parameter | Type | Mandatory | Description |
|---|---|---|---|
keyName | string | Yes | Index. |
function setStyles() {
const currentColor = localStorage.getItem('bgcolor');
const currentFont = localStorage.getItem('font');
const currentImage = localStorage.getItem('image');
document.getElementById('bgcolor').value = currentColor;
document.getElementById('font').value = currentFont;
document.getElementById('image').value = currentImage;
htmlElem.style.backgroundColor = `#${currentColor}`;
pElem.style.fontFamily = currentFont; imgElem.setAttribute('src', currentImage);
} Parameter | Type | Mandatory | Description |
|---|---|---|---|
keyName | string | Yes | Index. |
function populateStorage() {
localStorage.setItem('bgcolor', 'red');
localStorage.setItem('font', 'Helvetica');
localStorage.setItem('image', 'myCat.png');
localStorage.removeItem('image');
} Quick Game implements only a small number of Canvas 2D APIs for offscreen rendering of text and lines. To call any Canvas 2D API, create an offscreen canvas object in advance (let canvas = document.createElement('canvas');), and specify the width and height of the canvas (canvas.width = 100; and canvas.height = 100;) so that the drawing buffer size can be allocated.
Parameter | Description |
|---|---|
width | Width of the canvas, that is, the width of the drawing buffer. |
height | Height of the canvas, that is, the height of the drawing buffer. |
lineWidth | Line width. When a parameter value is obtained, the current value is returned. The default value is 1.0. Values such as 0, negative values, Infinity, and NaN are ignored when assigned to this parameter. |
font | Font style, for example, oblique small-caps 18px Arial. The default font is Arial and the default font size is 30.
A canvas supports only single-line font drawing. Setting the line height does not affect the font display. Therefore, line-height is not supported. |
textAlign | Horizontal alignment. The options are as follows:
|
textBaseline | Vertical alignment. The options are as follows:
|
fillStyle | Fill color. When the fillText API is called, the text is filled with the solid color. |
strokeStyle | Stroke color. When the strokeText API is called, this color is used as the text outline color. |
Method | Description |
|---|---|
Creates a path by deleting the subpath list. | |
Moves the start point of a subpath. | |
Returns from the current point to the start point of the current subpath. | |
Creates a quadratic Bezier curve. | |
Draws the current path. | |
Fills the current drawing (path). | |
Creates a rectangle. | |
Clears the rectangle area, and sets all pixels in a rectangle area to be transparent. The rectangle starts from (x, y) and has the size specified by (width, height). | |
Draws and fills a rectangle. The default fill color is black. | |
Measures and returns the dimensions of the text. | |
Draws the text at a specified position. | |
Fills a text with a solid color. | |
Zooms in or out on the current drawing. | |
Rotates the current drawing. | |
Remaps the point (0, 0) on the canvas. | |
Replaces the current transformation matrix. | |
Resets the current transformation matrix to an identity matrix. | |
Saves the current environment status to the stack. | |
Pops up the environment status on the top of the stack and restores the context to the status. | |
Quick Game implements the standard createImageData method of web APIs. For details, please refer to createImageData API Reference. | |
Quick Game implements the standard createImageData method of web APIs. For details, please refer to createImageData API Reference. | |
Quick Game implements the standard getImageData method of web APIs. For details, please refer to getImageData API Reference. | |
Quick Game implements the standard putImageData method of web APIs. For details, please refer to putImageData API Reference. | |
void putImageData(imagedata, dx, dy, dirtyX, dirtyY, dirtyWidth, dirtyHeight) | Quick Game implements the standard putImageData method of web APIs. For details, please refer to putImageData API Reference. |
let canvas = document.createElement('canvas');
// Set the width and height of the canvas to 100.
canvas.width = 100;
canvas.height = 100;
let ctx = canvas.getContext('2d');
ctx.strokeStyle = "rgb(255,0,0)";
// Fill the canvas with a color.
ctx.lineWidth = canvas.height;
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(canvas.width, 0);
ctx.lineTo(canvas.width, canvas.height);
ctx.lineTo(0, canvas.height);
ctx.stroke(); Parameter | Type | Mandatory | Description |
|---|---|---|---|
x | number | Yes | X coordinate of a point. |
y | number | Yes | Y coordinate of a point. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
cpx | number | Yes | X coordinate of the control point of the Bezier curve. |
cpy | number | Yes | Y coordinate of the control point of the Bezier curve. |
x | number | Yes | X coordinate of the end point. |
y | number | Yes | Y coordinate of the end point. |
Clears the rectangle area, and sets all pixels in a rectangle area to be transparent. The rectangle starts from (x, y) and has the size specified by (width, height).
Parameter | Type | Mandatory | Description |
|---|---|---|---|
x | number | Yes | X coordinate of the start point of a rectangle. |
y | number | Yes | Y coordinate of the start point of a rectangle. |
width | number | Yes | Width of a rectangle. |
height | number | Yes | Height of a rectangle. |
let canvas = this.canvas;
let ctx = canvas.getContext('2d');
ctx.clearRect(canvas.width / 2 - 75, canvas.height / 2 - 50, 150, 100);
ctx.stroke(); Parameter | Type | Mandatory | Description |
|---|---|---|---|
x | number | Yes | X coordinate of the start point of a rectangle. |
y | number | Yes | Y coordinate of the start point of a rectangle. |
width | number | Yes | Width of a rectangle. |
height | number | Yes | Height of a rectangle. |
var ctx = canvas2d.getContext("2d");
ctx.fillStyle ="rgba(255,255,255,0.5)";
ctx.fillRect(0, 0, 256, 1); Parameter | Type | Mandatory | Description |
|---|---|---|---|
text | Yes | Renders a text using the current values of font, textAlign, and textBaseline. | |
x | number | Yes | X coordinate of the text start point. |
y | number | Yes | Y coordinate of the text start point. |
maxWidth | number | No | Maximum width of the text to be drawn. If a value is assigned and the measured width of the text exceeds the value, the font is scaled horizontally or a smaller font is used. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
text | Yes | Renders a text using the current values of font, textAlign, and textBaseline. | |
x | number | Yes | X coordinate of the text start point. |
y | number | Yes | Y coordinate of the text start point. |
maxWidth | number | No | Maximum width of the text to be drawn. If a value is assigned and the measured width of the text exceeds the value, the font is scaled horizontally or a smaller font is used. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
scalewidth | number | Yes | Width scaling ratio. 1 equals 100%, 0.5 equals 50%, 2 equals 200%, and so on. |
scaleheight | number | Yes | Height scaling ratio. 1 equals 100%, 0.5 equals 50%, 2 equals 200%, and so on. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
angle | number | Yes | Rotation angle, in radians. To convert degrees to radians, use the following formula: Degrees x Math.PI/180. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
x | number | Yes | Value added to the X coordinate. |
y | number | Yes | Value added to the Y coordinate. |
Parameter | Type | Mandatory | Description |
|---|---|---|---|
a | number | Yes | Horizontal scaling. |
b | number | Yes | Horizontal skewing. |
c | number | Yes | Vertical skewing. |
d | number | Yes | Vertical scaling. |
e | number | Yes | Horizontal moving. |
f | number | Yes | Vertical moving. |
Quick Game implements the standard createImageData method of web APIs. For details, please refer to createImageData API Reference.
let imageData = this._context.createImageData(canvas.width, canvas.height / 2);
for (let i = 0; i < canvas.width; i += 1) {
for (let j = 0; j < canvas.height / 2; j += 1) {
let index = (j * canvas.width + i) * 4;
imageData.data[index + 0] = i % 255;
imageData.data[index + 1] = (i + j) % 255;
imageData.data[index + 2] = j % 255;
imageData.data[index + 3] = 255;
}
} Quick Game implements the standard getImageData method of web APIs. For details, please refer to getImageData API Reference.
Quick Game implements the standard putImageData method of web APIs. For details, please refer to putImageData API Reference.
Quick Game implements the standard web API Touch. For details, please refer to Touch API Reference.
Attributes not supported by Touch:
When a game is played in full screen, the values of screenX and screenY equal those of clientX and clientY, respectively.