# Open data domain basic capabilities

Developers can also directly use the basic capabilities to run without using the framework. The Open Data Domain is a closed and independent JavaScript scope. To make the code run in the open data domain, you need to add the configuration item openDataContext to the game.json to specify the code directory of the open data domain. Adding this configuration item means that the open data domain is enabled for the mini game, which will cause some restrictions.

{
  "deviceOrientation": "portrait",
  "openDataContext": "src/myOpenDataContext"
}

At the same time, index.js needs to be created in this directory as the entry file of the open data domain, and its code runs in the open data domain. game.js is the entry file of the entire game, and its code runs in the main domain. Corresponding to the above configuration, there should be the following directory structure:

├── src
| └── myOpenDataContext
| ├── index.js
| └── ...
├── game.js
├── game.json
└── ...

src/myOpenDataContext is the code directory of the open data domain. Except for src/myOpenDataContext, it is the code directory of the main domain.

The codes in the main domain and the open data domain cannot require each other. Take the following directory structure as an example:

├── src
| └── myOpenDataContext
| ├── index.js
| ├── util.js
| └── ...
├── lib
| └── render.js
└── game.js

Cannot require('src/myOpenDataContext/util') in game.js Cannot require('../../lib/render.js') in src/myOpenDataContext/index.js

# Communication between main domain and open data domain

The open data domain cannot send messages to the main domain.

The main domain can send messages to the open data domain. Call the wx.getOpenDataContext() method to obtain an open data domain instance, and call the OpenDataContext.postMessage() method on the instance to send a message to the open data domain.

// game.js
let openDataContext = wx.getOpenDataContext()
openDataContext.postMessage({
  text:'hello',
  year: (new Date()).getFullYear()
})

In the open data domain, you can monitor messages sent from the main domain through the wx.onMessage() method.

// src/myOpenDataContext/index.js
wx.onMessage(data => {
  console.log(data)
  /* {
    text:'hello',
    year: 2018
  } */
})

# Show relational chain data

If you want to display the user data obtained through the relationship chain API, such as drawing a leaderboard and other business scenarios, you need to draw the leaderboard on the sharedCanvas, and then render the sharedCanvas on the screen in the main domain.

// src/myOpenDataContext/index.js
let sharedCanvas = wx.getSharedCanvas()

function drawRankList (data) {
  data.forEach((item, index) => {
    // ...
  })
}

wx.getFriendCloudStorage({
  success: res => {
    let data = res.data
    drawRankList(data)
  }
})

sharedCanvas is an off-screen canvas that can be accessed by both the main domain and the open data domain. Calling wx.getSharedCanvas() in the open data domain will return sharedCanvas.

// src/myOpenDataContext/index.js
let sharedCanvas = wx.getSharedCanvas()
let context = sharedCanvas.getContext('2d')
context.fillStyle ='red'
context.fillRect(0, 0, 100, 100)

In the main domain, sharedCanvas can be accessed through the open data domain instance, and the sharedCanvas can be drawn to the upper screen canvas through the drawImage() method.

// game.js
let openDataContext = wx.getOpenDataContext()
let sharedCanvas = openDataContext.canvas

let canvas = wx.createCanvas()
let context = canvas.getContext('2d')
context.drawImage(sharedCanvas, 0, 0)

The width and height of sharedCanvas can only be set in the main domain, not in the open data domain.

// game.js
sharedCanvas.width = 400
sharedCanvas.height = 200

The sharedCanvas is essentially an off-screen Canvas, and resetting the width and height of the Canvas will clear the content on the Canvas. So the open data domain must be notified to redraw the sharedCanvas.

// game.js

openDataContext.postMessage({
  command:'render'
})
// src/myOpenDataContext/index.js
openDataContext.onMessage(data => {
  if (data.command ==='render') {
    // ... redraw sharedCanvas
  }
})

# limit

When the mini game starts the open data domain, that is, when the openDataContext configuration item is added to game.json. The mini game environment applies some restrictions to the main domain and open data domain.

# Primary domain

  1. The sharedCanvas can only be drawn on the upper screen canvas.
  2. The on-screen canvas cannot call toDataURL, and its context cannot call getImageData and readPixels.
  3. sharedCanvas cannot call toDataURL and getContext.
  4. Cannot draw on-screen canvas and sharedCanvas to other canvases in any form, including drawImage, createPattern, texImage2D, texSubImage2D.
  5. The width and height of sharedCanvas can only be set in the main domain

# Open Data Domain

  1. Cannot set the width and height of sharedCanvas

The open data domain can only call a limited API, as shown below:

Frame rate

-requestAnimationFrame() -cancelAnimationFrame()

Timer

-setTimeout() -clearTimeout() -setInterval() -clearInterval()

system message

-wx.getSystemInfo() -wx.getSystemInfoSync()

Touch Event

-wx.onTouchStart() -wx.onTouchMove() -wx.onTouchEnd() -wx.onTouchCancel() -wx.offTouchStart() -wx.offTouchMove() -wx.offTouchEnd() -wx.offTouchCancel()

canvas

-wx.createCanvas()

All canvases in the open data domain only support the 2d rendering mode, and cannot use toDataURL and toTempFilePath(Sync)

picture

-wx.createImage()

The image of the open data domain can only use the pictures from the local or WeChat CDN, and not the pictures on the developer's own server. For non-local or non-WeChat CDN pictures, you can first download the picture file from the main domain wx.downloadFile(), and then use OpenDataContext.postMessage() The file path is passed to the open data domain for use.

Open Data

-wx.getFriendCloudStorage() -wx.getGroupCloudStorage() -wx.getUserCloudStorage() -wx.setUserCloudStorage() -wx.removeUserCloudStorage()

Tips: For more interfaces, please check in API-Open Interface-Open Data

Monitor main domain messages

-wx.onMessage()

点击咨询小助手