Commit ac74cb04 authored by rockyl's avatar rockyl

增加是否能点交互的参数

parent ac856bdf
...@@ -16,13 +16,15 @@ const isMobile = (ua.indexOf('mobile') !== -1 || ua.indexOf('android') !== -1); ...@@ -16,13 +16,15 @@ const isMobile = (ua.indexOf('mobile') !== -1 || ua.indexOf('android') !== -1);
* @param options * @param options
*/ */
export function setupContext(options:any = {}) { export function setupContext(options:any = {}) {
const {canvas, touchHandler} = options; const {canvas, touchHandler, touchEnabled} = options;
_touchHandler = touchHandler; _touchHandler = touchHandler;
_canvas = canvas; _canvas = canvas;
addListeners(); if(touchEnabled){
addListeners();
}
} }
/** /**
...@@ -91,30 +93,26 @@ function addMouseListener() { ...@@ -91,30 +93,26 @@ function addMouseListener() {
*/ */
function addTouchListener() { function addTouchListener() {
_canvas.addEventListener("touchstart", (event) => { _canvas.addEventListener("touchstart", (event) => {
let l = event.changedTouches.length; for(let touch of event.changedTouches){
for (let i = 0; i < l; i++) { onTouchBegin(touch);
onTouchBegin(event.changedTouches[i]);
} }
prevent(event); prevent(event);
}, false); }, false);
_canvas.addEventListener("touchmove", (event) => { _canvas.addEventListener("touchmove", (event) => {
let l = event.changedTouches.length; for(let touch of event.changedTouches){
for (let i = 0; i < l; i++) { onTouchMove(touch);
onTouchMove(event.changedTouches[i]);
} }
prevent(event); prevent(event);
}, false); }, false);
_canvas.addEventListener("touchend", (event) => { _canvas.addEventListener("touchend", (event) => {
let l = event.changedTouches.length; for(let touch of event.changedTouches){
for (let i = 0; i < l; i++) { onTouchEnd(touch);
onTouchEnd(event.changedTouches[i]);
} }
prevent(event); prevent(event);
}, false); }, false);
_canvas.addEventListener("touchcancel", (event) => { _canvas.addEventListener("touchcancel", (event) => {
let l = event.changedTouches.length; for(let touch of event.changedTouches){
for (let i = 0; i < l; i++) { onTouchEnd(touch);
onTouchEnd(event.changedTouches[i]);
} }
prevent(event); prevent(event);
}, false); }, false);
...@@ -144,16 +142,31 @@ function onTouchEnd(event) { ...@@ -144,16 +142,31 @@ function onTouchEnd(event) {
_touchHandler.onTouchEnd(location); _touchHandler.onTouchEnd(location);
} }
function getLocation(event){
return pagePosToCanvasPos(event.pageX, event.pageY, event.identifier)
}
/** /**
* 获取当前点 * 页面坐标转画布坐标
* @param pageX
* @param pageY
* @param identifier
* @param isLocalPos
*/ */
function getLocation(event) { export function pagePosToCanvasPos(pageX, pageY, identifier?, isLocalPos:boolean = false) {
let doc = document.documentElement; let x = pageX, y = pageY;
let box = _canvas.getBoundingClientRect(); let box = _canvas.getBoundingClientRect();
let left = box.left + window.pageXOffset - doc.clientLeft; if(!isLocalPos){
let top = box.top + window.pageYOffset - doc.clientTop; let doc = document.documentElement;
let x = event.pageX - left, newX = x; let left = box.left + window.pageXOffset - doc.clientLeft;
let y = event.pageY - top, newY = y; let top = box.top + window.pageYOffset - doc.clientTop;
x = pageX - left;
y = pageY - top;
}
let newX = x;
let newY = y;
if (_rotation === 90) { if (_rotation === 90) {
newX = y; newX = y;
newY = box.width - x; newY = box.width - x;
...@@ -167,6 +180,23 @@ function getLocation(event) { ...@@ -167,6 +180,23 @@ function getLocation(event) {
return { return {
x: Math.round(newX), x: Math.round(newX),
y: Math.round(newY), y: Math.round(newY),
identifier: event.identifier || 0, identifier: identifier || 0,
}; };
} }
\ No newline at end of file
/**
* 画布坐标转页面坐标
* @param x
* @param y
*/
export function canvasPosToPagePos(x, y){
let doc = document.documentElement;
let box = _canvas.getBoundingClientRect();
x = x * _scaleX;
y = y * _scaleY;
return {
x, y,
}
}
...@@ -7,6 +7,7 @@ export {Entity} from './Entity' ...@@ -7,6 +7,7 @@ export {Entity} from './Entity'
export {Scene} from './Scene' export {Scene} from './Scene'
export {ScillaEvent} from './ScillaEvent' export {ScillaEvent} from './ScillaEvent'
export {getContext, createCanvas, getStageSize, getStageScale, getStageCenter, shortcut, ScaleMode} from './context/RenderContext'; export {getContext, createCanvas, getStageSize, getStageScale, getStageCenter, shortcut, ScaleMode} from './context/RenderContext';
export {pagePosToCanvasPos, canvasPosToPagePos} from './context/InteractContext';
export * from './manager' export * from './manager'
export {default as Texture, createTexture} from './Texture' export {default as Texture, createTexture} from './Texture'
......
...@@ -17,6 +17,7 @@ let options: any = { ...@@ -17,6 +17,7 @@ let options: any = {
designWidth: 750, designWidth: 750,
designHeight: 1334, designHeight: 1334,
scaleMode: ScaleMode.FIXED_WIDTH, scaleMode: ScaleMode.FIXED_WIDTH,
touchEnabled: true,
}; };
let root: Entity; let root: Entity;
...@@ -32,7 +33,7 @@ let lastFPS = 0; ...@@ -32,7 +33,7 @@ let lastFPS = 0;
export function setup(_options?) { export function setup(_options?) {
injectProp(options, _options); injectProp(options, _options);
const {canvas, designWidth, designHeight, scaleMode, modifyCanvasSize} = options; const {canvas, designWidth, designHeight, scaleMode, modifyCanvasSize, touchEnabled} = options;
let canvasElement = typeof canvas == 'object' ? canvas : document.getElementById(canvas); let canvasElement = typeof canvas == 'object' ? canvas : document.getElementById(canvas);
...@@ -42,8 +43,10 @@ export function setup(_options?) { ...@@ -42,8 +43,10 @@ export function setup(_options?) {
onTouchBegin, onTouchBegin,
onTouchMove, onTouchMove,
onTouchEnd, onTouchEnd,
} },
touchEnabled,
}); });
setupRenderContext({ setupRenderContext({
canvas: canvasElement, canvas: canvasElement,
designWidth, designWidth,
...@@ -82,6 +85,40 @@ export function getRoot(): Entity { ...@@ -82,6 +85,40 @@ export function getRoot(): Entity {
return root; return root;
} }
/**
* 获取节点路径
* @param entity
*/
export function getEntityPath(entity?: Entity): string {
let path = '';
let current = entity || root;
while (current.parent) {
path = current.parent.children.indexOf(current) + (path.length > 0 ? '|' : '') + path;
current = current.parent;
}
return path;
}
/**
* 根据节点路径获取节点
* @param path
*/
export function getEntityByPath(path?: string): Entity{
let target = root;
if (path.length > 0) {
let arr = path.split('|');
for (let item of arr) {
target = target.children[item];
if (!target) {
target = null;
break;
}
}
}
return target;
}
/** /**
* 获取当前帧率 * 获取当前帧率
*/ */
...@@ -128,6 +165,11 @@ function flush(tsNow): void { ...@@ -128,6 +165,11 @@ function flush(tsNow): void {
requestAnimationFrame(flush); requestAnimationFrame(flush);
} }
const nextTicks = [];
export function nextTick(func){
nextTicks.push(func);
}
function onFrameTick(tsNow) { function onFrameTick(tsNow) {
clear(); clear();
const tsNow2 = Date.now(); const tsNow2 = Date.now();
...@@ -145,6 +187,11 @@ function onFrameTick(tsNow) { ...@@ -145,6 +187,11 @@ function onFrameTick(tsNow) {
current.afterUpdate(); current.afterUpdate();
}); });
//const tsPass = Date.now() - tsNow; //const tsPass = Date.now() - tsNow;
while(nextTicks.length > 0){
let func = nextTicks.shift();
func();
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment