123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- import ListModule from "..";
- import { ColDataType } from "./item";
- export function InitCanvasDragEvent(
- this: ListModule,
- canvas: HTMLCanvasElement
- ) {
- let down = false;
- let downIndex = -1;
- let downY = 0;
- let downX = 0;
- let initOffset = 0;
- let downTime = 0;
- let moving = false;
- const fingers: { [name: number]: any } = {};
- const scrollType = this.store.animate.scrollType;
- const canvasData = this.store.canvas;
- const onMouseDown = (e: MouseEvent) => {
- const x = e.clientX * canvasData.scale;
- const y = e.clientY * canvasData.scale;
- downY = e.clientY * canvasData.scale;
- downX = e.clientX * canvasData.scale;
- down = true;
- downIndex =
- scrollType == "vertical"
- ? Math.floor(x / (canvasData.itemOffset + canvasData.padding))
- : Math.floor(y / (canvasData.itemOffset + canvasData.padding));
- canvasData.linesData[downIndex].dragging = true;
- initOffset = canvasData.linesData[downIndex].offset;
- downTime = Date.now();
- };
- const onMouseMove = (e: MouseEvent) => {
- if (!down || downIndex < 0) return;
- moving = true;
- console.log("move");
- let dtaOffset = 0;
- if (scrollType == "vertical") {
- dtaOffset = e.clientY * canvasData.scale - dtaOffset;
- }
- if (scrollType == "horizontal") {
- dtaOffset = e.clientX * canvasData.scale - dtaOffset;
- }
- canvasData.linesData[downIndex].offset = initOffset + dtaOffset;
- };
- const onMouseUp = (e: MouseEvent) => {
- console.log("up");
- down = false;
- if (downIndex >= 0) {
- canvasData.linesData[downIndex].dragging = false;
- const dtaTime = Date.now() - downTime;
- if (dtaTime < 200 && !moving) {
- const line = canvasData.linesData[downIndex];
- let offset = line.offset;
- if (offset >= line.halfOffset) offset -= line.halfOffset;
- if (offset < 0) offset += line.halfOffset;
- const n = line.items.length;
- let z =
- scrollType == "vertical"
- ? offset + canvasData.height
- : offset + canvasData.width;
- for (let k = 0; k < n; k++) {
- const item = line.items[k];
- if (scrollType == "vertical") {
- z -= item.offsetY;
- if (downY > z && downY < z + item.offsetY) {
- console.log("click->", item.name);
- this.actions.popDialog(e, item);
- break;
- }
- }
- if (scrollType == "horizontal") {
- z -= item.offsetX;
- if (downX > z && downX < z + item.offsetX) {
- console.log("click->", item.name);
- this.actions.popDialog(e, item);
- break;
- }
- }
- }
- }
- }
- downIndex = -1;
- moving = false;
- };
- const onTouchstart = (e: TouchEvent) => {
- const touches = e.touches;
- for (let i = 0; i < touches.length; i++) {
- const touch = touches[i];
- if (fingers[touch.identifier]) {
- continue;
- }
- const x = touch.clientX * canvasData.scale;
- const y = touch.clientY * canvasData.scale;
- const downIndex =
- scrollType == "vertical"
- ? Math.floor(x / (canvasData.itemOffset + canvasData.padding))
- : Math.floor(y / (canvasData.itemOffset + canvasData.padding));
- canvasData.linesData[downIndex].dragging = true;
- const offset = canvasData.linesData[downIndex].offset;
- fingers[touch.identifier] = {
- identifier: touch.identifier,
- downY: y,
- downX: x,
- downIndex: downIndex,
- offset: offset,
- downTime: Date.now(),
- };
- }
- };
- const onTouchMove = (e: TouchEvent) => {
- const touches = e.changedTouches;
- for (let i = 0; i < touches.length; i++) {
- const touch = touches[i];
- const finger = fingers[touch.identifier];
- if (!finger) {
- continue;
- }
- finger.moving = true;
- let dtaOffset = 0;
- if (scrollType == "vertical") {
- dtaOffset = touch.clientY * canvasData.scale - finger.downY;
- }
- if (scrollType == "horizontal") {
- dtaOffset = touch.clientX * canvasData.scale - finger.downY;
- }
- canvasData.linesData[finger.downIndex].offset = finger.offset + dtaOffset;
- }
- };
- const onTouchEnd = (e: TouchEvent) => {
- const touches = e.changedTouches;
- for (let i = 0; i < touches.length; i++) {
- const touch = touches[i];
- const finger = fingers[touch.identifier];
- if (!finger) {
- continue;
- }
- canvasData.linesData[finger.downIndex].dragging = false;
- const dtaTime = Date.now() - finger.downTime;
- if (dtaTime < 200 && !finger.moving) {
- const line = canvasData.linesData[finger.downIndex];
- let offset = line.offset;
- if (offset >= line.halfOffset) offset -= line.halfOffset;
- if (offset < 0) offset += line.halfOffset;
- const n = line.items.length;
- let z =
- scrollType == "vertical"
- ? offset + canvasData.height
- : offset + canvasData.width;
- for (let k = 0; k < n; k++) {
- const item = line.items[k];
- if (scrollType == "vertical") {
- z -= item.offsetY;
- if (finger.downY > z && finger.downY < z + item.offsetY) {
- this.actions.popDialog(touch, item);
- break;
- }
- }
- if (scrollType == "horizontal") {
- z -= item.offsetX;
- if (finger.downX > z && finger.downX < z + item.offsetX) {
- this.actions.popDialog(touch, item);
- break;
- }
- }
- }
- }
- delete fingers[touch.identifier];
- }
- };
- canvas.addEventListener("mousedown", onMouseDown, false);
- canvas.addEventListener("mousemove", onMouseMove, false);
- document.addEventListener("mouseup", onMouseUp, false);
- canvas.addEventListener("touchstart", onTouchstart, false);
- canvas.addEventListener("touchmove", onTouchMove, false);
- document.addEventListener("touchend", onTouchEnd, false);
- }
- export function ListDraw(
- this: ListModule,
- line: ColDataType,
- row: number,
- ctx: any
- ) {
- const scrollType = this.store.animate.scrollType;
- const canvasData = this.store.canvas;
- let x = 0,
- y = 0;
- const offseStep = canvasData.itemOffset + canvasData.padding;
- if (scrollType == "vertical") {
- x =
- row > 0
- ? Math.floor(row * offseStep) + canvasData.padding
- : canvasData.padding;
- y = line.offset + canvasData.height;
- }
- if (scrollType == "horizontal") {
- x = line.offset + canvasData.width;
- y =
- row > 0
- ? Math.floor(row * offseStep) + canvasData.padding
- : canvasData.padding;
- }
- const items = line.items;
- const total = items.length;
- for (let k = 0; k < total; k++) {
- //绘制
- const item = items[k];
- if (scrollType == "vertical") {
- y -= item.offsetY; //从下往上绘制
- if (y > canvasData.height || y < -item.offsetY) continue; //超出部分,不进行绘制提交
- ctx.drawImage(
- item.img,
- 0,
- 0,
- item.w,
- item.h,
- Math.floor(x),
- Math.floor(y),
- Math.floor(canvasData.itemOffset),
- Math.floor(item.offsetY - canvasData.padding)
- );
- }
- if (scrollType == "horizontal") {
- x -= item.offsetX;
- if (x > canvasData.width || x < -item.offsetX) continue;
- ctx.drawImage(
- item.img,
- 0,
- 0,
- item.w,
- item.h,
- Math.floor(x),
- Math.floor(y),
- Math.floor(item.offsetX - canvasData.padding),
- Math.floor(canvasData.itemOffset)
- );
- }
- // let text =
- // item.name.length > 20
- // ? item.name.substring(0, 19) + "..."
- // : item.name;
- // ctx.fillText(text, x, Math.round(y + item.offsetY - textH * 1.2));
- }
- }
|