wechat-miniprogram / minigame-canvas-engine

轻量级canvas2d渲染引擎,开放数据域开发解决方案。
https://wechat-miniprogram.github.io/minigame-canvas-engine/
MIT License
273 stars 63 forks source link

对列表中格子点击事件不准确 #6

Closed lhkzh closed 3 years ago

lhkzh commented 3 years ago

在电脑上手机上都有偏差,手机上因屏幕不同偏差不同

yuanzm commented 3 years ago

是不是没有正确调用updateViewPort函数,这个是事件正确处理的大前提

lhkzh commented 3 years ago

https://wechat-miniprogram.github.io/minigame-canvas-engine/playground.html 这个例子里面。在pc上点击也有偏差。 js部分,修改了下随机内容的nickname部分标识,添加了click打印。 ` let xmlValue = window.xml.getValue(); let styleValue = window.style.getValue(); let styleObj = eval(styleValue);

// 创建mock数据 let item = { nickname: "zim5", rankScore: 1, avatarUrl: 'https://res.wx.qq.com/wechatgame/product/webpack/userupload/20191119/wegoing.jpeg', }; let datasource = { data :[], selfIndex: 1, self : item } for ( let i = 0; i < 20;i++ ) { var cp = JSON.parse(JSON.stringify(item)); cp.nickname = "zim"+(i+1); cp.rankScore = Math.floor(Math.random()*1000+1) datasource.data.push(cp); }

// 将XML模板编译成XML字符串 let tempFn = window.doT.template(xmlValue); let resultText = tempFn(datasource);

function init() { // getElementPagePosition为IDE内置函数 let pos = window.getElementPagePosition(canvas); // 每次初始化之前先执行清理逻辑保证内存不会一直增长 Layout.clear(); // 初始化引擎 Layout.init(resultText, styleObj);

//console.log(Layout)
// 设置canvas的尺寸和样式的container比例一致
canvas.width = Layout.renderport.width;
canvas.height = Layout.renderport.height;
canvas.style.width = 300 + 'px';
canvas.style.height = canvas.height / canvas.width * 300 + 'px';

Layout.updateViewPort({
    x     : pos.x,
    y     : pos.y,
    width : canvas.offsetWidth,
    height: canvas.offsetHeight,
});

Layout.layout(context);
var arr=Layout.getElementsByClassName('listItem');
arr.forEach((item,ii) => {
    console.log(ii,JSON.stringify(item.realLayoutBox))
    item.on('click', (e) => {
        console.log( item.children[2].value);
        //console.log("listItem:", item);
    });
});

}

init(); window.onresize = init;`

liuxin2533 commented 3 years ago

在电脑上手机上都有偏差,手机上因屏幕不同偏差不同

请问这个问题解决了吗

yuanzm commented 3 years ago

已经修复