Skip to content

Commit

Permalink
fix(line-pattern): 修复linewidth>=spacing 时,绘制异常问题 (#2801)
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelii authored and visiky committed Aug 19, 2021
1 parent 27b17ed commit c08e879
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
10 changes: 2 additions & 8 deletions examples/plugin/pattern/demo/bar-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ const PATTERN_MAP = {
},
分类二: {
type: 'square',
cfg: {
size: 5,
padding: 0,
rotation: 0,
isStagger: true,
},
},
分类三: {
type: 'line',
},
分类四: {
type: 'square',
cfg: {
size: 5,
size: 4,
padding: 1,
rotation: 45,
isStagger: false,
Expand All @@ -36,7 +30,7 @@ const PATTERN_MAP = {
分类五: {
type: 'line',
cfg: {
spacing: 6,
spacing: 3,
lineWidth: 2,
rotation: 90,
},
Expand Down
10 changes: 5 additions & 5 deletions src/utils/pattern/line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { initCanvas, drawBackground, transformMatrix, getPixelRatio } from './ut
*/
export const defaultLinePatternCfg = {
rotation: 45,
spacing: 5,
spacing: 4,
opacity: 1,
backgroundColor: 'transparent',
strokeOpacity: 0.5,
Expand All @@ -28,7 +28,7 @@ export function drawLine(context: CanvasRenderingContext2D, cfg: LinePatternCfg,

context.globalAlpha = strokeOpacity;
context.lineCap = 'square';
context.strokeStyle = stroke;
context.strokeStyle = lineWidth ? stroke : 'transparent';
context.lineWidth = lineWidth;
context.stroke(path);
}
Expand All @@ -39,11 +39,11 @@ export function drawLine(context: CanvasRenderingContext2D, cfg: LinePatternCfg,
export function createLinePattern(cfg?: LinePatternCfg): CanvasPattern {
const lineCfg = deepAssign({}, defaultLinePatternCfg, cfg);

const { spacing, rotation } = lineCfg;
const { spacing, rotation, lineWidth } = lineCfg;

// 计算 pattern 画布的大小, path 所需的 d
const width = spacing;
const height = spacing;
const width = spacing + lineWidth || 1;
const height = spacing + lineWidth || 1;
const d = `
M 0 0 L ${width} 0
M 0 ${height} L ${width} ${height}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/pattern/square.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import {
* squarePattern 的 默认配置
*/
export const defaultSquarePatternCfg = {
size: 4,
padding: 1,
isStagger: false,
size: 5,
padding: 0,
isStagger: true,
backgroundColor: 'transparent',
opacity: 1,
rotation: 0,
Expand Down

0 comments on commit c08e879

Please sign in to comment.