Commit ac74cb04 authored by rockyl's avatar rockyl

增加是否能点交互的参数

parent ac856bdf
......@@ -16,13 +16,15 @@ const isMobile = (ua.indexOf('mobile') !== -1 || ua.indexOf('android') !== -1);
* @param options
*/
export function setupContext(options:any = {}) {
const {canvas, touchHandler} = options;
const {canvas, touchHandler, touchEnabled} = options;
_touchHandler = touchHandler;
_canvas = canvas;
addListeners();
if(touchEnabled){
addListeners();
}
}
/**
......@@ -91,30 +93,26 @@ function addMouseListener() {
*/
function addTouchListener() {
_canvas.addEventListener("touchstart", (event) => {
let l = event.changedTouches.length;
for (let i = 0; i < l; i++) {
onTouchBegin(event.changedTouches[i]);
for(let touch of event.changedTouches){
onTouchBegin(touch);
}
prevent(event);
}, false);
_canvas.addEventListener("touchmove", (event) => {
let l = event.changedTouches.length;
for (let i = 0; i < l; i++) {
onTouchMove(event.changedTouches[i]);
for(let touch of event.changedTouches){
onTouchMove(touch);
}
prevent(event);
}, false);
_canvas.addEventListener("touchend", (event) => {
let l = event.changedTouches.length;
for (let i = 0; i < l; i++) {
onTouchEnd(event.changedTouches[i]);
for(let touch of event.changedTouches){
onTouchEnd(touch);
}
prevent(event);
}, false);
_canvas.addEventListener("touchcancel", (event) => {
let l = event.changedTouches.length;
for (let i = 0; i < l; i++) {
onTouchEnd(event.changedTouches[i]);
for(let touch of event.changedTouches){
onTouchEnd(touch);
}
prevent(event);
}, false);
......@@ -144,16 +142,31 @@ function onTouchEnd(event) {
_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) {
let doc = document.documentElement;
export function pagePosToCanvasPos(pageX, pageY, identifier?, isLocalPos:boolean = false) {
let x = pageX, y = pageY;
let box = _canvas.getBoundingClientRect();
let left = box.left + window.pageXOffset - doc.clientLeft;
let top = box.top + window.pageYOffset - doc.clientTop;
let x = event.pageX - left, newX = x;
let y = event.pageY - top, newY = y;
if(!isLocalPos){
let doc = document.documentElement;
let left = box.left + window.pageXOffset - doc.clientLeft;
let top = box.top + window.pageYOffset - doc.clientTop;
x = pageX - left;
y = pageY - top;
}
let newX = x;
let newY = y;
if (_rotation === 90) {
newX = y;
newY = box.width - x;
......@@ -167,6 +180,23 @@ function getLocation(event) {
return {
x: Math.round(newX),
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'
export {Scene} from './Scene'
export {ScillaEvent} from './ScillaEvent'
export {getContext, createCanvas, getStageSize, getStageScale, getStageCenter, shortcut, ScaleMode} from './context/RenderContext';
export {pagePosToCanvasPos, canvasPosToPagePos} from './context/InteractContext';
export * from './manager'
export {default as Texture, createTexture} from './Texture'
......
......@@ -17,6 +17,7 @@ let options: any = {
designWidth: 750,
designHeight: 1334,
scaleMode: ScaleMode.FIXED_WIDTH,
touchEnabled: true,
};
let root: Entity;
......@@ -32,7 +33,7 @@ let lastFPS = 0;
export function setup(_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);
......@@ -42,8 +43,10 @@ export function setup(_options?) {
onTouchBegin,
onTouchMove,
onTouchEnd,
}
},
touchEnabled,
});
setupRenderContext({
canvas: canvasElement,
designWidth,
......@@ -82,6 +85,40 @@ export function getRoot(): Entity {
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 {
requestAnimationFrame(flush);
}
const nextTicks = [];
export function nextTick(func){
nextTicks.push(func);
}
function onFrameTick(tsNow) {
clear();
const tsNow2 = Date.now();
......@@ -145,6 +187,11 @@ function onFrameTick(tsNow) {
current.afterUpdate();
});
//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