Skip to content

Commit

Permalink
fix: fontHeight计算调整
Browse files Browse the repository at this point in the history
  • Loading branch information
jinfeiyang committed Mar 17, 2021
1 parent 515521b commit ece3d75
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 13 deletions.
3 changes: 2 additions & 1 deletion dist/easy-canvas.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/easy-canvas.min.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion example/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,15 @@ function RadioGroup(c, { attrs, styles, on }, content) {
const onItemClick = item => {
selected && selected.setStyles({
borderColor: '#ccc',
borderWidth: 0.5
borderWidth: 0.5,
width:14,
height:14
})
item.setStyles({
borderColor: THEME.PRIMARY,
borderWidth: 4,
width:6,
height:6
})
value = item.options.attrs.value
selected = item
Expand Down
3 changes: 2 additions & 1 deletion example/weapp/easy-canvas.min.js

Large diffs are not rendered by default.

20 changes: 15 additions & 5 deletions lib/canvas-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export default class CanvasRender {
this.throttle = getThrottle(16)
}

isDebug(){
return this.getLayer().options && this.getLayer().options.debug
}

getCtx() {
return this.layer.ctx
}
Expand Down Expand Up @@ -212,8 +216,8 @@ export default class CanvasRender {
}

// for debug
if (this.getLayer().options && this.getLayer().options.debug) {
this.getCtx().strokeStyle = 'green'
if (this.isDebug()) {
this.getCtx().strokeStyle = element.debugColor || 'green'
this.getCtx().strokeRect(element.contentX, element.contentY, element.renderStyles.contentWidth, element.renderStyles.contentHeight)
// ctx.strokeStyle = '#fff'
// ctx.strokeText(`${parseInt(this.contentX)} ${parseInt(this.contentY)} ${contentWidth} ${contentHeight}`, this.contentX + 100, this.contentY + 10)
Expand Down Expand Up @@ -288,7 +292,10 @@ export default class CanvasRender {
} else {
_x = x
}
_y = (element.contentY + (lineHeight - element._layout.fontHeight) / 2 + lineHeight * index) - 1
_y = (element.contentY + ((lineHeight - element._layout.fontHeight) / 2) + lineHeight * index)
if(this.isDebug()){
this.getCtx().fillRect(_x,_y,2,2)
}
this.getCtx().fillText(line.text, _x, _y)
if (isWX() && element.renderStyles.fontWeight !== 400) {
// 小程序 字体加粗不生效
Expand Down Expand Up @@ -323,13 +330,16 @@ export default class CanvasRender {
*/
measureText(element, text) {
let w = 0
let h = 0
this._restore(() => {
this.getCtx().font = element._getFont()
const { width } = this.getCtx().measureText(text)
const { width,fontBoundingBoxAscent } = this.getCtx().measureText(text)
w = width
h = fontBoundingBoxAscent || element.renderStyles.fontSize * 0.8
})
return {
width: w
width: w,
fontHeight:h
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Text extends Element {
this._lines = []
this.children += ''
this.type = 'text'
this.debugColor = 'blue'
}

_paint() {
Expand All @@ -28,7 +29,6 @@ export default class Text extends Element {

_measureLayout() {
this._layout = this.getRender().measureText(this, this.children)
this._layout.fontHeight = this.renderStyles.fontSize * 0.8
this._layout.height = this.renderStyles.lineHeight
this._calcLine()
return this._layout
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "easy-canvas-layout",
"version": "0.1.13",
"version": "0.1.14",
"description": "A canvas lib help to easy layout in canvas.",
"miniprogram": "lib",
"main": "./lib/index.js",
Expand All @@ -25,4 +25,4 @@
"rollup": "^2.28.2",
"rollup-plugin-terser": "^7.0.0"
}
}
}

0 comments on commit ece3d75

Please sign in to comment.