mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-12-14 00:58:45 +00:00
[adapters] 增加小游戏适配部分源码
This commit is contained in:
77
adapters/platforms/taobao-minigame/wrapper/builtin/Canvas.js
Normal file
77
adapters/platforms/taobao-minigame/wrapper/builtin/Canvas.js
Normal file
@@ -0,0 +1,77 @@
|
||||
import { innerWidth, innerHeight } from './WindowProperties'
|
||||
|
||||
function Canvas () {}
|
||||
|
||||
let CanvasProxy = new Proxy(Canvas, {
|
||||
construct () {
|
||||
|
||||
const canvas = my.createCanvas()
|
||||
|
||||
canvas.type = 'canvas'
|
||||
|
||||
// canvas.__proto__.__proto__.__proto__ = new HTMLCanvasElement()
|
||||
|
||||
const _getContext = canvas.getContext
|
||||
|
||||
canvas.getBoundingClientRect = () => {
|
||||
const ret = {
|
||||
top: 0,
|
||||
left: 0,
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
canvas.style = {
|
||||
top: '0px',
|
||||
left: '0px',
|
||||
width: innerWidth + 'px',
|
||||
height: innerHeight + 'px',
|
||||
}
|
||||
|
||||
canvas.addEventListener = function (type, listener, options = {}) {
|
||||
// console.log('canvas.addEventListener', type);
|
||||
if (my.isIDE) {
|
||||
$global.document.addEventListener(type, listener, options);
|
||||
}
|
||||
}
|
||||
|
||||
canvas.removeEventListener = function (type, listener) {
|
||||
// console.log('canvas.removeEventListener', type);
|
||||
if (my.isIDE) {
|
||||
$global.document.removeEventListener(type, listener);
|
||||
}
|
||||
}
|
||||
|
||||
canvas.dispatchEvent = function (event = {}) {
|
||||
// console.log('canvas.dispatchEvent' , event.type, event);
|
||||
if (my.isIDE) {
|
||||
$global.document.dispatchEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
Object.defineProperty(canvas, 'clientWidth', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return innerWidth
|
||||
}
|
||||
})
|
||||
|
||||
Object.defineProperty(canvas, 'clientHeight', {
|
||||
enumerable: true,
|
||||
get: function get() {
|
||||
return innerHeight
|
||||
}
|
||||
})
|
||||
|
||||
return canvas
|
||||
},
|
||||
});
|
||||
|
||||
// Expose the global canvas
|
||||
$global.screencanvas = $global.screencanvas || new CanvasProxy();
|
||||
// NOTE: this is a hack operation
|
||||
// let canvas = new window.Canvas()
|
||||
// console.error(canvas instanceof window.Canvas) => false
|
||||
export default CanvasProxy;
|
||||
Reference in New Issue
Block a user