1.0.3 • Published 6 years ago

react-resize-svg v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

React Resize HOC Components

基于React实现的可以拖拽和缩放的SVG组件库,组件库核心两个组件:ResizeSvgResizeSvgHOC。其中ResizeSvgHOC是高级组件,作用是实现组件包装和Props传递。

README_EN

效果图

npm.io

Document

ResizeSvg & ResizeSvgHOC

Resize SVG 盒子模型分析

ResizeSvg 如下图显示,分为两块部分:操作区域(ResizeWidth)和显示区域(ContentWidth)。其中padding也是可操作点(四角的圆点)的直径(强调:是直径),默认值:8

npm.io

属性 API

Attribute NametypeDescription
paddingnumber间距 默认值:8
widthnumber宽度,必须是数值。单位:px。但是不支持:100px方式设置
heightnumber宽度,必须是数值。单位:px。但是不支持:100px方式设置

样式 API

ClassName AttributeDescription
svgContainerStylesvg tag style
showLineStyleshow line style (default dasharray)
showCircleStyleshow circle style
triggerLineStyletrigger line style
triggerCircleStyletrigger circle style
triggerMoveRectStyletrigger move rect style

注入的 Props

通过ResizeSvgHOC包含后的子组件,将会自动注入一下Props

Prop NametypeDescription
paddingnumberpadding
widthnumberSVG 真实宽度
heightnumberSVG 真实高度
contentWidthnumber可显示区域内容宽度
contentWidthnumber可显示区域内容高度

使用

  • npm 安装
npm install react-resize-svg
  • 开发组件

基于ResizeSvgHOC 开发一个自定义的图形组件。例如:

import React, { Component } from "react";
import ResizeSvgHOC from "./ResizeSvgHOC";

class EllipseSvg extends Component {
	render() {
    let { padding, contentWidth, contentHeight } = this.props;
		return (
			<ellipse
				cx={contentWidth / 2 + padding}
        cy={contentHeight / 2 + padding}
        rx={contentWidth/2}
        ry={contentHeight/2}
				style={{ fill: "green" }}
			/>
		);
	}
}
// 注意HOC方式导出
export default ResizeSvgHOC(EllipseSvg);

使用ResizeSvgHOC导出组件后,会新增三个props:padding,contentWidthcontentHeight 方便计算和使用。(上面文章的:Resize SVG 盒子模型分析)

不能直接使用 ResizeSvg 然后加入 svg的子标签方式(eg: <rect/>, <path/> <ellipse/>)。

  • 自定义属性(含样式属性style , className

    	- 自定义组件内部定义模式
    
    		```JSX
    		import React, { Component } from "react";
    		import ResizeSvgHOC from "./ResizeSvgHOC";
    
    		class EllipseSvg extends Component {
    			render() {
    				let { padding, contentWidth, contentHeight } = this.props;
    				return (
    					<ellipse
    						cx={contentWidth / 2 + padding}			
    						cy={contentHeight / 2 + padding}
    						rx={contentWidth/2}
    						ry={contentHeight/2}
    						style={{ fill: "green" }}
    					/>
    				);
    			}
    		}
    		export default ResizeSvgHOC(EllipseSvg);
    		```
    
    	- 自定义组件传参模式
    		```
    		<RectSvg
    			width="100"
    			height="100"
    			top="10"
    			left="10"
    			rx="20"
    			ry="20"
    			style={{ fill: "red" }}
    			className={style.customClass}
    		/>
    		```

Q&A

  • 加载ResizeSvg图形的外部容器,position必须是absolute

    	图形的缩放会影响所在图层的大小,如果采用默认布局,缩放图形的时候,会影响整体文档流,从而影响其他图形的位置。
    
    	```JSX
    	// container position absolute
    	<div style={{ position: "absolute" }}>
    		<RectSvg
    			width="100"
    			height="100"
    			top="10"
    			left="10"
    			style={{ fill: "red" }}
    			className={style.customClass}
    		/>
    	</div>
    	```

开发调试

  • 开发
git clone 

npm i 

npm run start
  • 发布
npm run build

npm.io

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago