CRender
What is CRender?
- It's a vector graphics rendering plugin based on canvas.
- It provides animation and mouse event support for graphics.
Install with npm
$ npm install @jiaminghi/c-render
Detailed documents and examples can be viewed on the HomePage.
Usage
import CRender from '@jiaminghi/c-redner'
const canvas = document.getElementById('canvas')
const render = new CRender(canvas)
const circle = render.add({ name: 'circle', ... })
Class CRender
Instantiation
class CRender {
}
attribute
ctx
area
animationStatus
graphs
prototype
add
CRender.prototype.add = function (config = {}) {
}
Clone
CRender.prototype.clone = function (graph) {
}
delGraph
CRender.prototype.delGraph = function (graph) {
}
delAllGraph
CRender.prototype.delAllGraph = function () {
}
drawAllGraph
CRender.prototype.drawAllGraph = function () {
}
clearArea
CRender.prototype.clearArea = function () {
}
launchAnimation
CRender.prototype.launchAnimation = function () {
}
Class Graph
attribute
When adding a graph, you can configure them.
visible
shape
drag
hover
index
animationDelay
animationFrame
animationPause
hoverRect
mouseEnter
mouseOuter
click
Tip
Enable mouseEnter, mouseOuter, click event support requires configuring the hover
property of the graph to true
. Extended new graph require the hoverCheck method to be configured to provide event support.
prototype
attr
Graph.prototype.attr = function (attrName, change = undefined) {
}
animation
Graph.prototype.animation = async function (attrName, change, wait = false) {
}
animationEnd
Graph.prototype.animationEnd = function () {
}
pauseAnimation
Graph.prototype.pauseAnimation = function () {
}
playAnimation
Graph.prototype.playAnimation = function () {
}
Life Cycle
When you add graph to the render, you can configure the following functions in the configuration, they will be called at a specific time.
- added
- beforeDraw
- drawed
- beforeMove
- moved
- beforeDelete
- deleted
Tip
The current graph instance will be passed in as a parameter,and the mousemove event will also be passed to beforeMove and moved.
Class Style
attribute
fill
stroke
opacity
lineCap
lineJoin
lineDash
lineDashOffset
shadowBlur
shadowColor
shadowOffsetX
shadowOffsetY
lineWidth
strokeNoScale
graphCenter
scale
rotate
translate
hoverCursor
fontStyle
fontVarient
fontWeight
fontSize
fontFamily
textAlign
textBaseline
gradientColor
gradientType
gradientParams
gradientWith
gradientStops
colors
Tip
Gradient is automatically enabled when gradientColor
and gradientParams
are configured.
prototype
getStyle
Style.prototype.getStyle = function () {
}
Examples
CRender provides some basic vector graph, examples are as follows.
circle
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center r-axis coordinate. |
r | Number | 0 | Circle radius. |
const { area: [w, h] } = render
const circleConfig = {
name: 'circle',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
r: 50
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('shape', { r: 70 }, true)
this.animation('style', { shadowBlur: 20 })
},
mouseOuter (e) {
this.animation('shape', { r: 50 }, true)
this.animation('style', { shadowBlur: 0 })
}
}
const circle = render.add(circleConfig)
ellipse
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center y-axis coordinate. |
hr | Number | 0 | Horizontal axis radius. |
vr | Number | 0 | Vertical axis radius. |
const { area: [w, h] } = render
const ellipseConfig = {
name: 'ellipse',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
hr: 80,
vr: 30
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
shadowColor: '#66eece',
scale: [1, 1],
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { scale: [1.5, 1.5], shadowBlur: 20 })
},
mouseOuter (e) {
this.animation('style', { scale: [1, 1], shadowBlur: 0 })
}
}
const ellipse = render.add(ellipseConfig)
rect
shape attribute
Attribute name | Type | Default | Annotation |
---|
x | Number | 0 | The x coordinate of the top left corner of the rectangle. |
y | Number | 0 | The y coordinate of the top left corner of the rectangle. |
w | Number | 0 | Rectangle width. |
h | Number | 0 | Rectangle height. |
const { area: [w, h] } = render
const rectConfig = {
name: 'rect',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
x: w / 2 - rectWidth / 2,
y: h / 2 - rectHeight / 2,
w: rectWidth,
h: rectHeight
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer',
translate: [0, 0]
},
mouseEnter (e) {
this.animation('shape', { w: 400 }, true)
this.animation('style', { shadowBlur: 20, translate: [-100, 0] })
},
mouseOuter (e) {
this.animation('shape', { w: 200 }, true)
this.animation('style', { shadowBlur: 0, translate: [0, 0] })
}
}
const rect = render.add(rectConfig)
ring
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center y-axis coordinate. |
r | Number | 0 | Ring radius. |
const { area: [w, h] } = render
const ringConfig = {
name: 'ring',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
r: 50
},
style: {
stroke: '#9ce5f4',
lineWidth: 20,
hoverCursor: 'pointer',
shadowBlur: 0,
shadowColor: '#66eece'
},
mouseEnter (e) {
this.animation('style', { shadowBlur: 20, lineWidth: 30 })
},
mouseOuter (e) {
this.animation('style', { shadowBlur: 0, lineWidth: 20 })
}
}
const ring = render.add(ringConfig)
arc
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center y-axis coordinate. |
r | Number | 0 | Arc radius. |
startAngle | Number | 0 | Arc start angle. |
endAngle | Number | 0 | Arc end angle. |
clockWise | Boolean | true | Clockwise |
const { area: [w, h] } = render
const arcConfig = {
name: 'arc',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
r: 60,
startAngle: 0,
endAngle: Math.PI / 3
},
style: {
stroke: '#9ce5f4',
lineWidth: 20,
shadowBlur: 0,
rotate: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('shape', { endAngle: Math.PI }, true)
this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
},
mouseOuter (e) {
this.animation('shape', { endAngle: Math.PI / 3 }, true)
this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
}
}
const arc = render.add(arcConfig)
sector
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center y-axis coordinate. |
r | Number | 0 | Sector radius. |
startAngle | Number | 0 | Sector start angle. |
endAngle | Number | 0 | Sector end angle. |
clockWise | Boolean | true | Clockwise |
const { area: [w, h] } = render
const sectorConfig = {
name: 'sector',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
r: 60,
startAngle: 0,
endAngle: Math.PI / 3
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
rotate: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('shape', { endAngle: Math.PI, r: 70 }, true)
this.animation('style', { shadowBlur: 20, rotate: -30, lineWidth: 30 })
},
mouseOuter (e) {
this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
this.animation('style', { shadowBlur: 0, rotate: 0, lineWidth: 20 })
}
}
const sector = render.add(sectorConfig)
regPolygon
shape attribute
Attribute name | Type | Default | Annotation |
---|
rx | Number | 0 | Center x-axis coordinate. |
ry | Number | 0 | Center y-axis coordinate. |
r | Number | 0 | Circumradius. |
side | Number | 0 | Edge number. |
const { area: [w, h] } = render
const regPolygonConfig = {
name: 'regPolygon',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
rx: w / 2,
ry: h / 2,
r: 60,
side: 6
},
style: {
fill: '#9ce5f4',
hoverCursor: 'pointer',
shadowBlur: 0,
rotate: 0,
shadowColor: '#66eece'
},
mouseEnter (e) {
this.animation('shape', { endAngle: Math.PI, r: 100 }, true)
this.animation('style', { shadowBlur: 20, rotate: 180 })
},
mouseOuter (e) {
this.animation('shape', { endAngle: Math.PI / 3, r: 60 }, true)
this.animation('style', { shadowBlur: 0, rotate: 0 })
}
}
const regPolygon = render.add(regPolygonConfig)
polyline
shape attribute
Attribute name | Type | Default | Annotation |
---|
points | Array | [] | The points that makes up the polyline. |
close | Boolean | false | Whether to close the polyline. |
const { area: [w, h] } = render
const top = h / 3
const bottom = h / 3 * 2
const gap = w / 10
const beginX = w / 2 - gap * 2
const points = new Array(5).fill('').map((t, i) =>
[beginX + gap * i, i % 2 === 0 ? top : bottom])
const polylineConfig = {
name: 'polyline',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points
},
style: {
stroke: '#9ce5f4',
shadowBlur: 0,
lineWidth: 10,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { lineWidth: 20, shadowBlur: 20 })
},
mouseOuter (e) {
this.animation('style', { lineWidth: 10, shadowBlur: 0 })
}
}
const polyline = render.add(polylineConfig)
polyline (closed)
const { area: [w, h] } = render
const top = h / 3
const bottom = h / 3 * 2
const gap = w / 10
const beginX = w / 2 - gap * 2
const points = new Array(5).fill('').map((t, i) =>
[beginX + gap * i, i % 2 === 0 ? top : bottom])
points[2][1] += top * 1.3
const polylineClosedConfig = {
name: 'polyline',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points,
close: true
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
lineWidth: 10,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { shadowBlur: 20 }, true)
const pointsCloned = deepClone(this.shape.points)
pointsCloned[2][1] += top * 0.3
this.animation('shape', { points: pointsCloned })
},
mouseOuter (e) {
this.animation('style', { shadowBlur: 0 }, true)
const pointsCloned = deepClone(this.shape.points)
pointsCloned[2][1] -= top * 0.3
this.animation('shape', { points: pointsCloned })
}
}
const polylineClosed = render.add(polylineClosedConfig)
smoothline
shape attribute
Attribute name | Type | Default | Annotation |
---|
points | Array | [] | The points that makes up the smoothline. |
close | Boolean | false | Whether to close the smoothline. |
const { area: [w, h] } = render
const top = h / 3
const bottom = h / 3 * 2
const gap = w / 10
const beginX = w / 2 - gap * 2
const points = new Array(5).fill('').map((t, i) =>
[beginX + gap * i, i % 2 === 0 ? top : bottom])
const smoothlineConfig = {
name: 'smoothline',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points
},
style: {
stroke: '#9ce5f4',
shadowBlur: 0,
lineWidth: 10,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { lineWidth: 20, shadowBlur: 20 })
},
mouseOuter (e) {
this.animation('style', { lineWidth: 10, shadowBlur: 0 })
}
}
const smoothline = render.add(smoothlineConfig)
smoothline (closed)
import { getCircleRadianPoint } from '../../CRender/lib/util'
function getPoints (radius, centerPoint, pointNum) {
const PIDived = Math.PI * 2 / pointNum
const points = new Array(pointNum).fill('')
.map((foo, i) =>
getCircleRadianPoint(...centerPoint, radius, PIDived * i)
)
return points
}
const { area: [w, h] } = render
const radius = h / 3
const centerPoint = [w / 2, h / 2]
const smoothlineClosedConfig = {
name: 'smoothline',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points: getPoints(radius, centerPoint, 3),
close: true
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
lineWidth: 10,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { lineWidth: 20, shadowBlur: 20, rotate: 120 })
},
mouseOuter (e) {
this.animation('style', { lineWidth: 10, shadowBlur: 0, rotate: 0 })
},
setGraphCenter (e, { style }) {
if (e) {
const { movementX, movementY } = e
const [cx, cy] = style.graphCenter
style.graphCenter = [cx + movementX, cy + movementY]
} else {
style.graphCenter = [...centerPoint]
}
}
}
const smoothlineClosed = render.add(smoothlineClosedConfig)
bezierCurve
shape attribute
Attribute name | Type | Default | Annotation |
---|
points | Array | [] | The points that makes up the bezierCurve. |
close | Boolean | false | Whether to close the smoothline. |
const { area: [w, h] } = render
const offsetX = w / 2
const offsetY = h / 2
const points = [
[-100 + offsetX, -50 + offsetY],
[
[0 + offsetX, -50 + offsetY],
[0 + offsetX, 50 + offsetY],
[100 + offsetX, 50 + offsetY]
],
]
const bezierCurveConfig = {
name: 'bezierCurve',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points
},
style: {
lineWidth: 10,
stroke: '#9ce5f4',
shadowBlur: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e) {
this.animation('style', { lineWidth: 20, shadowBlur: 20 })
},
mouseOuter (e) {
this.animation('style', { lineWidth: 10, shadowBlur: 0 })
}
}
const bezierCurve = render.add(bezierCurveConfig)
bezierCurve (closed)
import { getCircleRadianPoint } from '../../CRender/lib/util'
function getPetalPoints (insideRadius, outsideRadius, petalNum, petalCenter) {
const PI2Dived = Math.PI * 2 / (petalNum * 3)
let points = new Array(petalNum * 3).fill('')
.map((foo, i) =>
getCircleRadianPoint(...petalCenter,
i % 3 === 0 ? insideRadius : outsideRadius,
PI2Dived * i)
)
const startPoint = points.shift()
points.push(startPoint)
points = new Array(petalNum).fill('')
.map(foo => points.splice(0, 3))
points.unshift(startPoint)
return points
}
const { area: [w, h] } = render
const petalCenter = [w / 2, h / 2]
const [raidus1, raidus2, raidus3, raidus4] = [h / 6, h / 2.5, h / 3, h / 2]
const bezierCurveClosedConfig = {
name: 'bezierCurve',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
shape: {
points: getPetalPoints(raidus1, raidus2, 6, petalCenter),
close: true
},
style: {
fill: '#9ce5f4',
shadowBlur: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer'
},
mouseEnter (e, { style: { graphCenter } }) {
this.animation('style', { lineWidth: 20, shadowBlur: 20 }, true)
this.animation('shape', { points: getPetalPoints(raidus3, raidus4, 6, graphCenter) })
},
mouseOuter (e, { style: { graphCenter } }) {
this.animation('style', { lineWidth: 10, shadowBlur: 0 }, true)
this.animation('shape', { points: getPetalPoints(raidus1, raidus2, 6, graphCenter) })
},
setGraphCenter (e, { style }) {
if (e) {
const { movementX, movementY } = e
const [cx, cy] = style.graphCenter
style.graphCenter = [cx + movementX, cy + movementY]
} else {
style.graphCenter = [...petalCenter]
}
}
}
const bezierCurveClosed = render.add(bezierCurveClosedConfig)
text
shape attribute
Attribute name | Type | Default | Annotation |
---|
content | String | '' | Text content. |
position | Array | [0, 0] | Text start position. |
maxWidth | Number | Undefined | Maximum width of the text. |
const { area: [w, h] } = render
const centerPoint = [w / 2, h / 2]
const hoverRect = [w / 2 - 100, h / 2 - 30 ,200, 60]
const textConfig = {
name: 'text',
animationCurve: 'easeOutBack',
hover: true,
drag: true,
hoverRect,
shape: {
content: 'CRender',
position: centerPoint,
maxWidth: 200
},
style: {
fill: '#9ce5f4',
fontSize: 50,
shadowBlur: 0,
rotate: 0,
shadowColor: '#66eece',
hoverCursor: 'pointer',
scale: [1, 1],
rotate: 0
},
mouseEnter (e) {
this.animation('style', { shadowBlur: 20, scale: [1.5, 1.5], rotate: 30 })
},
mouseOuter (e) {
this.animation('style', { shadowBlur: 0, scale: [1, 1], rotate: 0 })
},
moved (e, { hoverRect }) {
const { movementX, movementY } = e
hoverRect[0] += movementX
hoverRect[1] += movementY
}
}
const text = render.add(textConfig)
Tip
Graph of text should be configured with hoverRect
to support mouse events.
Extend New Graph
CRender provides a Function to extend new graph,you can customize the graphics you want.
import { extendNewGraph } from '@jiaminghi/c-render'
const graphName = 'newGraph'
const graphConfig = {
shape: { ... },
}
extendNewGraph(graphName, graphConfig)
extendNewGraph
function extendNewGraph (name, config) {
}
Graph Configuration Attribute
shape (required)
validator (required)
draw (required)
hoverCheck
setGraphCenter
move
example of extend new graph
import { extendNewGraph } from '@jiaminghi/c-render'
const circle = {
shape: {
rx: 0,
ry: 0,
r: 0
},
validator ({ shape }) {
const { rx, ry, r } = shape
if (typeof rx !== 'number' || typeof ry !== 'number' || typeof r !== 'number') {
console.error('Shape configuration is abnormal!')
return false
}
return true
},
draw ({ ctx }, { shape }) {
ctx.beginPath()
const { rx, ry, r } = shape
ctx.arc(rx, ry, r, 0, Math.PI * 2)
ctx.fill()
ctx.stroke()
ctx.closePath()
},
hoverCheck (position, { shape }) {
const { rx, ry, r } = shape
return checkPointIsInCircle(rx, ry, r, position)
},
setGraphCenter (e, { shape, style }) {
const { rx, ry } = shape
style.graphCenter = [rx, ry]
},
move ({ movementX, movementY }, { shape }) {
this.attr('shape', {
rx: shape.rx + movementX,
ry: shape.ry + movementY
})
}
}
extendNewGraph('circle', circle)
Related support
-
Transition
Provide animation transition data(animationCurve).
If you want to extend the new animation curve, you can use the method provided by transition.
import { injectNewCurve } from '@jiaminghi/c-render'
injectNewCurve('newCurve', curveData)
-
BezierCurve
Provides support for bezierCurve, such as curve length calculation, conversion between curve and polyline.
-
Color
Provides calculation of color values.