generated from mengxinssfd/dumi-monorepo-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from js-tool-pack/image
Image
- Loading branch information
Showing
7 changed files
with
201 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* title: 基础用法 | ||
*/ | ||
|
||
import { Renderer, Image } from '@tool-pack/canvas'; | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
const App: React.FC = () => { | ||
const elRef = useRef<HTMLCanvasElement>(null); | ||
|
||
useEffect(() => { | ||
const el = elRef.current; | ||
if (!el) return; | ||
|
||
const renderer = new Renderer(el); | ||
const img = new Image( | ||
'https://s.cn.bing.net/th?id=OHR.BangkokCircle_ZH-CN4702412806_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp', | ||
{ | ||
borderRadius: 20, | ||
height: 100, | ||
width: 100, | ||
top: 100, | ||
left: 0, | ||
}, | ||
); | ||
// 划过时切换图片 | ||
img.addEventListener('mouseenter', function () { | ||
this.src = | ||
'https://s.cn.bing.net/th?id=OHR.ArenalCostaRica_ZH-CN4466297855_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp'; | ||
}); | ||
img.addEventListener('mouseleave', function () { | ||
this.src = | ||
'https://s.cn.bing.net/th?id=OHR.BangkokCircle_ZH-CN4702412806_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp'; | ||
}); | ||
|
||
const images = [ | ||
new Image( | ||
'https://s.cn.bing.net/th?id=OHR.TarragonaSpain_ZH-CN5488361711_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp', | ||
{ | ||
height: 100, | ||
width: 100, | ||
}, | ||
), | ||
new Image( | ||
'https://s.cn.bing.net/th?id=OHR.WahclellaFalls_ZH-CN4932852217_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp', | ||
{ | ||
borderColor: 'deeppink', | ||
borderRadius: 20, | ||
borderWidth: 5, | ||
height: 100, | ||
width: 100, | ||
left: 100, | ||
top: 0, | ||
}, | ||
), | ||
img, | ||
new Image( | ||
'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1610%2F20%2Fc16%2F28666491_1476977210753.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1639854808&t=8c31d1b0991a4c8a5068e9f303c1fe90', | ||
{ | ||
borderColor: '#00e09e', | ||
borderWidth: 5, | ||
height: 100, | ||
width: 100, | ||
left: 100, | ||
top: 100, | ||
}, | ||
), | ||
] as const; | ||
|
||
images.forEach((img) => renderer.add(img)); | ||
renderer.render(); | ||
|
||
return () => renderer.destroy(); | ||
}, []); | ||
|
||
return ( | ||
<canvas | ||
style={{ border: '1px solid pink' }} | ||
draggable="false" | ||
height={200} | ||
width={200} | ||
ref={elRef} | ||
/> | ||
); | ||
}; | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
category: Core | ||
title: Image 图片 | ||
atomId: Image | ||
nav: | ||
title: Core | ||
demo: | ||
cols: 2 | ||
group: | ||
title: 基础 | ||
--- | ||
|
||
Image 图片 | ||
|
||
## 代码演示 | ||
|
||
<!-- prettier-ignore --> | ||
<code src="./demo/basic.tsx"></code> | ||
|
||
## API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Rectangle } from '~/rectangle'; | ||
import { Style } from '~/Style'; | ||
|
||
export class Image extends Rectangle { | ||
private img!: HTMLImageElement; | ||
private _src!: string; | ||
crossOrigin: string | null = null; | ||
|
||
constructor(src?: string, style?: Partial<Style>) { | ||
super(style); | ||
if (src) this._src = src; | ||
this.createImgElement(); | ||
} | ||
|
||
private drawImg(ctx: CanvasRenderingContext2D): void { | ||
const pattern = ctx.createPattern(this.img, 'no-repeat'); | ||
const { left = 0, top = 0, height, width } = this.computedStyle; | ||
const { naturalHeight, naturalWidth } = this.img; | ||
|
||
this.drawPathWithoutBorder(ctx); | ||
if (pattern) { | ||
// 注意:translate 和 scale 的先后顺序会影响实际位置,必须先移动再缩放 | ||
const matrix = new DOMMatrix() | ||
.translate(left, top) | ||
.scale(width / naturalWidth, height / naturalHeight); | ||
|
||
pattern.setTransform(matrix); | ||
ctx.fillStyle = pattern; | ||
ctx.fill(); | ||
} | ||
} | ||
|
||
private createImgElement(): void { | ||
const img = (this.img = document.createElement('img')); | ||
img.crossOrigin = this.crossOrigin; | ||
img.onload = () => { | ||
this.computedStyle.width ||= img.width; | ||
this.computedStyle.height ||= img.height; | ||
this.update(); | ||
}; | ||
img.onerror = img.oncancel = (): void => { | ||
console.error(`'${this._src}' load fail`); | ||
}; | ||
img.src = this._src; | ||
} | ||
override render(ctx: CanvasRenderingContext2D): void { | ||
// super.render(ctx); | ||
this.renderBackground(ctx); | ||
this.drawImg(ctx); | ||
this.renderBorder(ctx); | ||
} | ||
set src(src: string) { | ||
this._src = src; | ||
this.img.src = src; | ||
} | ||
get src(): string { | ||
return this._src; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ demo: | |
cols: 2 | ||
group: | ||
order: 0 | ||
title: 形状 | ||
title: 基础 | ||
--- | ||
|
||
Rectangle 矩形 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters