123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- import { getRgbaValue, getColorFromRgbValue } from '../color'
- import { deepClone } from '../plugin/util'
- export default class Style {
- constructor (style) {
- this.colorProcessor(style)
- const defaultStyle = {
-
- fill: [0, 0, 0, 1],
-
- stroke: [0, 0, 0, 0],
-
- opacity: 1,
-
- lineCap: null,
-
- lineJoin: null,
-
- lineDash: null,
-
- lineDashOffset: null,
-
- shadowBlur: 0,
-
- shadowColor: [0, 0, 0, 0],
-
- shadowOffsetX: 0,
-
- shadowOffsetY: 0,
-
- lineWidth: 0,
-
- graphCenter: null,
-
- scale: null,
-
- rotate: null,
-
- translate: null,
-
- hoverCursor: 'pointer',
-
- fontStyle: 'normal',
-
- fontVarient: 'normal',
-
- fontWeight: 'normal',
-
- fontSize: 10,
-
- padding:0,
- lineHeight:1,
- wrap:true,
- ellipsis:false,
- letterSpacing:0,
- fontFamily: 'Arial',
- textDecoration:'none',
-
- textAlign: 'left',
-
- textBaseline: 'middle',
-
- gradientColor: null,
-
- gradientType: 'linear',
-
- gradientParams: null,
-
- gradientWith: 'stroke',
-
- gradientStops: 'auto',
-
- colors: null
- }
- Object.assign(this, defaultStyle, style)
- }
- }
- Style.prototype.colorProcessor = function (style, reverse = false) {
- const processor = reverse ? getColorFromRgbValue : getRgbaValue
- const colorProcessorKeys = ['fill', 'stroke', 'shadowColor']
- const allKeys = Object.keys(style)
- const colorKeys = allKeys.filter(key => colorProcessorKeys.find(k => k === key))
- colorKeys.forEach(key => (style[key] = processor(style[key])))
- const { gradientColor, colors } = style
- if (gradientColor) style.gradientColor = gradientColor.map(c => processor(c))
- if (colors) {
- const colorsKeys = Object.keys(colors)
- colorsKeys.forEach(key => (colors[key] = processor(colors[key])))
- }
- }
- Style.prototype.initStyle = function (ctx,shape) {
- initTransform(ctx, this)
- initGraphStyle(ctx, this)
- initGradient(ctx, this,shape)
- }
- function initTransform (ctx, style) {
- ctx.save()
- const { graphCenter, rotate, scale, translate } = style
- if (!(graphCenter instanceof Array)) return
- if(graphCenter.length>0) ctx.translate(...graphCenter);
- if (rotate) ctx.rotate(rotate * Math.PI / 180)
- if (scale instanceof Array) ctx.scale(...scale)
- if (translate) ctx.translate(...translate)
- ctx.translate(-graphCenter[0], -graphCenter[1])
- }
- const autoSetStyleKeys = [
- 'lineCap', 'lineJoin', 'lineDashOffset',
- 'shadowOffsetX', 'shadowOffsetY', 'lineWidth',
- 'textAlign', 'textBaseline'
- ]
- function initGraphStyle (ctx, style) {
- let { fill, stroke, shadowColor, opacity } = style
- autoSetStyleKeys.forEach(key => {
- if (key || typeof key === 'number') ctx[key] = style[key]
- })
- fill = [...fill]
- stroke = [...stroke]
- shadowColor = [...shadowColor]
- fill[3] *= opacity
- stroke[3] *= opacity
- shadowColor[3] *= opacity
-
- ctx.fillStyle = getColorFromRgbValue(fill)
- ctx.strokeStyle = getColorFromRgbValue(stroke)
- ctx.shadowColor = getColorFromRgbValue(shadowColor)
- let { lineDash, shadowBlur } = style
- if (lineDash) {
- lineDash = lineDash.map(v => v >= 0 ? v : 0)
- ctx.setLineDash(lineDash)
- }
- if (typeof shadowBlur === 'number') ctx.shadowBlur = shadowBlur > 0 ? shadowBlur : 0.001
- const { fontStyle, fontVarient, fontWeight, fontSize, fontFamily } = style
- ctx.font = fontStyle + ' ' + fontVarient + ' ' + fontWeight + ' ' + fontSize + 'px' + ' ' + fontFamily
- }
- function initGradient (ctx, style,shape) {
- if (!gradientValidator(style)) return
-
- let { gradientColor, gradientParams, gradientType, gradientWith, gradientStops, opacity } = style
- gradientColor = gradientColor.map(color => {
- let colorOpacity = color[3] * opacity
- let clonedColor = [...color]
- clonedColor[3] = colorOpacity
- return clonedColor
- })
- gradientColor = gradientColor.map(c => getColorFromRgbValue(c))
- if (gradientStops === 'auto') gradientStops = getAutoColorStops(gradientColor)
- let gra = [...gradientParams];
- if(gradientType =='linear'){
- gra[0] =shape.x + gradientParams[0]
- gra[1] =shape.y + gradientParams[1]
- gra[2] =shape.x +gradientParams[2]
- gra[3] =shape.y +gradientParams[3]
- }
-
- const gradient = ctx[`create${gradientType.slice(0, 1).toUpperCase() + gradientType.slice(1)}Gradient`](...gra)
- gradientStops.forEach((stop, i) => gradient.addColorStop(stop, gradientColor[i]))
- ctx[`${gradientWith}Style`] = gradient
- }
- function gradientValidator (style) {
- const { gradientColor, gradientParams, gradientType, gradientWith, gradientStops } = style
- if (!gradientColor || !gradientParams) return false
- if (gradientColor.length === 1) {
- console.warn('The gradient needs to provide at least two colors')
- return false
- }
- if (gradientType !== 'linear' && gradientType !== 'radial') {
- console.warn('GradientType only supports linear or radial, current value is ' + gradientType)
- return false
- }
- const gradientParamsLength = gradientParams.length
- if (
- (gradientType === 'linear' && gradientParamsLength !== 4) ||
- (gradientType === 'radial' && gradientParamsLength !== 6)
- ) {
- console.warn('The expected length of gradientParams is ' + (gradientType === 'linear' ? '4' : '6'))
- return false
- }
- if (gradientWith !== 'fill' && gradientWith !== 'stroke') {
- console.warn('GradientWith only supports fill or stroke, current value is ' + gradientWith)
- return false
- }
- if (gradientStops !== 'auto' && !(gradientStops instanceof Array)) {
- console.warn(`gradientStops only supports 'auto' or Number Array ([0, .5, 1]), current value is ` + gradientStops)
- return false
- }
- return true
- }
- function getAutoColorStops (color) {
- const stopGap = 1 / (color.length - 1)
- return color.map((foo, i) => stopGap * i)
- }
- Style.prototype.restoreTransform = function (ctx) {
- ctx.restore()
- }
- Style.prototype.update = function (change) {
- this.colorProcessor(change)
- Object.assign(this, change)
- }
- Style.prototype.getStyle = function () {
- const clonedStyle = deepClone(this, true)
- this.colorProcessor(clonedStyle, true)
- return clonedStyle
- }
|