Skip to content

Commit 4df68a1

Browse files
committed
Init
0 parents  commit 4df68a1

File tree

8 files changed

+281
-0
lines changed

8 files changed

+281
-0
lines changed

LICENCE

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
MIT License
3+
4+
Copyright (c) 2018-2019 SeinJS (https://github.com/SeinJS)
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# seinjs-my-tiny-program-component
2+
3+
用于适配阿里小程序的组件,一般配合CLI创建模板使用,详见[Sein.js-扩展容器-阿里小程序](http://seinjs.com/cn/extension/containers/my-tiny-program)

injectCanvas.js

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
"use strict";
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.injectCanvas = injectCanvas;
7+
exports.handleEvent = handleEvent;
8+
9+
/**
10+
* @File : injectCanvas.js
11+
* @Author :dtysky ([email protected])
12+
* @Date : 7/29/2019, 7:26:37 PM
13+
* @Description:
14+
*/
15+
var LISTENERS = {
16+
click: [],
17+
touchstart: [],
18+
touchend: [],
19+
touchmove: [],
20+
touchcancel: []
21+
};
22+
23+
function clearListeners() {
24+
Object.keys(LISTENERS).forEach(function (key) {
25+
LISTENERS[key] = [];
26+
});
27+
}
28+
29+
function injectCanvas(canvas, options) {
30+
clearListeners();
31+
var dpi = options.dpi;
32+
canvas.style = {};
33+
34+
canvas.getBoundingClientRect = function () {
35+
var bound = {
36+
x: 0,
37+
y: 0,
38+
width: canvas.width / dpi,
39+
height: canvas.height / dpi,
40+
left: 0,
41+
right: canvas.width / dpi,
42+
top: 0,
43+
bottom: canvas.height / dpi
44+
};
45+
return bound;
46+
};
47+
48+
canvas.addEventListener = function (type, listener) {
49+
if (!LISTENERS[type]) {
50+
return;
51+
}
52+
53+
LISTENERS[type].push(listener);
54+
};
55+
56+
canvas.removeEventListener = function (type, listener) {
57+
if (!LISTENERS[type]) {
58+
return;
59+
}
60+
61+
var index = LISTENERS[type].indexOf(listener);
62+
63+
if (index >= 0) {
64+
LISTENERS[type].splice(index, 1);
65+
}
66+
};
67+
68+
my.window.mainCanvas = canvas;
69+
return canvas;
70+
}
71+
72+
var noop = function noop() {};
73+
74+
function convertTouch(touch) {
75+
var x = touch.x,
76+
y = touch.y,
77+
identifier = touch.identifier;
78+
return {
79+
identifier: identifier,
80+
x: x,
81+
y: y,
82+
clientX: x,
83+
clientY: y,
84+
pageX: x,
85+
pageY: y,
86+
screenX: x,
87+
screenY: y
88+
};
89+
}
90+
91+
function convertEvent(event) {
92+
var e = {
93+
preventDefault: noop,
94+
stopPropagation: noop,
95+
target: event.target,
96+
currentTarget: event.currentTarget
97+
};
98+
99+
if (event.type === 'tap') {
100+
var _event$detail = event.detail,
101+
x = _event$detail.x,
102+
y = _event$detail.y;
103+
e.x = x;
104+
e.y = y;
105+
e.clientX = x;
106+
e.clientY = y;
107+
e.offsetX = x;
108+
e.offsetY = y;
109+
e.pageX = x;
110+
e.pageY = y;
111+
e.screenX = x;
112+
e.screenY = y;
113+
} else {
114+
e.changedTouches = event.changedTouches.map(convertTouch);
115+
e.touches = event.touches.map(convertTouch);
116+
}
117+
118+
return e;
119+
}
120+
121+
function handleEvent(type, event) {
122+
if (!LISTENERS[type]) {
123+
return;
124+
}
125+
126+
var e = convertEvent(event);
127+
LISTENERS[type].forEach(function (cb) {
128+
return cb(e);
129+
});
130+
}

package.json

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "seinjs-my-tiny-program-component",
3+
"version": "0.8.4",
4+
"description": "A ali tiny program component for using Sein.js.",
5+
"main": "seinjs.js",
6+
"scripts": {},
7+
"repository": {
8+
"type": "git",
9+
"url": "[email protected]:SeinJS/Sein.js.git"
10+
},
11+
"keywords": [
12+
"Game engine",
13+
"Sein.js",
14+
"SeinJS",
15+
"Tiny Program"
16+
],
17+
"authors": [
18+
{
19+
"name": "Tianyu Dai (dtysky)",
20+
"email": "[email protected]"
21+
}
22+
],
23+
"license": "MIT",
24+
"bugs": {
25+
"url": "http://github.com/SeinJS/Sein.js/issues"
26+
},
27+
"files": [
28+
"README.md",
29+
"seinjs.js",
30+
"seinjs.json",
31+
"seinjs.acss",
32+
"seinjs.axml",
33+
"injectCanvas.js",
34+
"LICENCE"
35+
],
36+
"homepage": "http://github.com/SeinJS/Sein.js#readme",
37+
"devDependencies": {},
38+
"dependencies": {}
39+
}

seinjs.acss

Whitespace-only changes.

seinjs.axml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<view class="sein-container">
2+
<canvas
3+
disable-scroll
4+
type="webgl"
5+
id="canvas"
6+
class="sein-canvas"
7+
width="{{width*dpi}}"
8+
height="{{height*dpi}}"
9+
style="width:{{width}}px;height:{{height}}px;display:block;"
10+
enableNative="{{enableNative}}"
11+
onTouchStart="handleTouchStart"
12+
onTouchEnd="handleTouchEnd"
13+
onTouchMove="handleTouchMove"
14+
onTouchCancel="handleTouchCancel"
15+
onTap="handleClick"
16+
/>
17+
</view>

seinjs.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"use strict";
2+
3+
var _injectCanvas = require("./injectCanvas");
4+
5+
/**
6+
* @File : index.js
7+
* @Author :dtysky ([email protected])
8+
* @Date : 2019/7/25 上午11:06:59
9+
* @Description:
10+
*/
11+
Component({
12+
data: {
13+
width: 0,
14+
height: 0,
15+
dpi: 2,
16+
enableNative: true
17+
},
18+
props: {
19+
width: 0,
20+
height: 0,
21+
onSeinCanvasCreated: function onSeinCanvasCreated(canvas) {}
22+
},
23+
canvas: null,
24+
didMount: function didMount() {
25+
var _this = this;
26+
27+
var _my$window = my.window,
28+
h = _my$window.innerHeight,
29+
w = _my$window.innerWidth,
30+
dpi = _my$window.pixelRatio;
31+
var data = {
32+
width: this.props.width || (isNaN(w) ? parseInt(w) : w),
33+
height: this.props.height || (isNaN(h) ? parseInt(h) : h),
34+
dpi: isNaN(dpi) ? parseInt(dpi) : dpi
35+
};
36+
this.setData(data, function () {
37+
my._createCanvas({
38+
id: 'canvas',
39+
// canvas dom id
40+
success: function success(canvas) {
41+
_this.canvas = (0, _injectCanvas.injectCanvas)(canvas, _this.data);
42+
43+
_this.props.onSeinCanvasCreated(_this.canvas);
44+
}
45+
});
46+
});
47+
},
48+
didUpdate: function didUpdate() {},
49+
didUnmount: function didUnmount() {},
50+
methods: {
51+
handleTouchStart: function handleTouchStart(event) {
52+
(0, _injectCanvas.handleEvent)('touchstart', event);
53+
},
54+
handleTouchEnd: function handleTouchEnd(event) {
55+
(0, _injectCanvas.handleEvent)('touchend', event);
56+
},
57+
handleTouchMove: function handleTouchMove(event) {
58+
(0, _injectCanvas.handleEvent)('touchmove', event);
59+
},
60+
handleTouchCancel: function handleTouchCancel(event) {
61+
(0, _injectCanvas.handleEvent)('touchcancel', event);
62+
},
63+
handleClick: function handleClick(event) {
64+
(0, _injectCanvas.handleEvent)('click', event);
65+
}
66+
}
67+
});

seinjs.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"component": true
3+
}

0 commit comments

Comments
 (0)