* 初始化截图canvas * @param {number} totalWidth - 总宽度 * @param {number} totalHeight - 总高度 * @returns {Array} - 初始化的canvas数组 * @private
(totalWidth, totalHeight)
| 77 | * @private |
| 78 | */ |
| 79 | function _initScreenshots(totalWidth, totalHeight) { |
| 80 | // 检查尺寸是否超过限制 |
| 81 | const badSize = (totalHeight > MAX_PRIMARY_DIMENSION || |
| 82 | totalWidth > MAX_PRIMARY_DIMENSION || |
| 83 | totalHeight * totalWidth > MAX_AREA); |
| 84 | const biggerWidth = totalWidth > totalHeight; |
| 85 | |
| 86 | // 计算每个分块的最大尺寸 |
| 87 | const maxWidth = (!badSize ? totalWidth : |
| 88 | (biggerWidth ? MAX_PRIMARY_DIMENSION : MAX_SECONDARY_DIMENSION)); |
| 89 | const maxHeight = (!badSize ? totalHeight : |
| 90 | (biggerWidth ? MAX_SECONDARY_DIMENSION : MAX_PRIMARY_DIMENSION)); |
| 91 | |
| 92 | // 计算分块数量 |
| 93 | const numCols = Math.ceil(totalWidth / maxWidth); |
| 94 | const numRows = Math.ceil(totalHeight / maxHeight); |
| 95 | |
| 96 | // 创建结果数组 |
| 97 | const result = []; |
| 98 | let canvasIndex = 0; |
| 99 | |
| 100 | // 创建所有需要的canvas |
| 101 | for (let row = 0; row < numRows; row++) { |
| 102 | for (let col = 0; col < numCols; col++) { |
| 103 | const canvas = document.createElement('canvas'); |
| 104 | canvas.width = (col === numCols - 1 ? totalWidth % maxWidth || maxWidth : maxWidth); |
| 105 | canvas.height = (row === numRows - 1 ? totalHeight % maxHeight || maxHeight : maxHeight); |
| 106 | |
| 107 | const left = col * maxWidth; |
| 108 | const top = row * maxHeight; |
| 109 | |
| 110 | result.push({ |
| 111 | canvas: canvas, |
| 112 | ctx: canvas.getContext('2d'), |
| 113 | index: canvasIndex, |
| 114 | left: left, |
| 115 | right: left + canvas.width, |
| 116 | top: top, |
| 117 | bottom: top + canvas.height |
| 118 | }); |
| 119 | |
| 120 | canvasIndex++; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return result; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * 从截屏中筛选有效数据 |
no test coverage detected