Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
m0_68066342
uni-app
提交
c9a2c894
U
uni-app
项目概览
m0_68066342
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c9a2c894
编写于
4月 28, 2019
作者:
d-u-a
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update: canvas更新宽高后无效问题,canvasToTempFilePath宽高及目标宽高计算错误问题
上级
49c44e17
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
27 deletion
+51
-27
src/core/helpers/hidpi.js
src/core/helpers/hidpi.js
+1
-4
src/core/service/api/context/canvas.js
src/core/service/api/context/canvas.js
+43
-19
src/core/view/components/canvas/index.vue
src/core/view/components/canvas/index.vue
+7
-4
未找到文件。
src/core/helpers/hidpi.js
浏览文件 @
c9a2c894
const
pixelRatio
=
(
function
()
{
export
const
pixelRatio
=
(
function
()
{
const
canvas
=
document
.
createElement
(
'
canvas
'
)
const
context
=
canvas
.
getContext
(
'
2d
'
)
const
backingStore
=
context
.
backingStorePixelRatio
||
...
...
@@ -163,7 +163,4 @@ export function wrapper (canvas) {
canvas
.
width
*=
pixelRatio
canvas
.
height
*=
pixelRatio
canvas
.
getContext
(
'
2d
'
).
__hidpi__
=
true
console
.
log
(
canvas
.
width
)
console
.
log
(
canvas
.
height
)
}
src/core/service/api/context/canvas.js
浏览文件 @
c9a2c894
import
createCallbacks
from
'
uni-helpers/callbacks
'
import
{
wrapper
}
from
'
uni-helpers/hidpi
'
import
{
wrapper
,
pixelRatio
}
from
'
uni-helpers/hidpi
'
const
canvasEventCallbacks
=
createCallbacks
(
'
canvasEvent
'
)
...
...
@@ -255,10 +255,16 @@ var methods3 = ['setFillStyle', 'setTextAlign', 'setStrokeStyle', 'setGlobalAlph
]
var
tempCanvas
function
getTempCanvas
()
{
function
getTempCanvas
(
width
,
height
)
{
if
(
!
tempCanvas
)
{
tempCanvas
=
document
.
createElement
(
'
canvas
'
)
wrapper
(
tempCanvas
)
}
if
(
typeof
width
!==
'
undefined
'
&&
typeof
height
!==
'
undefined
'
)
{
if
(
tempCanvas
.
width
!==
width
||
tempCanvas
.
height
!==
height
)
{
tempCanvas
.
width
=
width
/
pixelRatio
tempCanvas
.
height
=
height
/
pixelRatio
wrapper
(
tempCanvas
)
}
}
return
tempCanvas
}
...
...
@@ -812,6 +818,12 @@ export function canvasToTempFilePath ({
fileType
,
qualit
},
callbackId
)
{
if
(
typeof
width
!==
'
undefined
'
)
{
width
*=
pixelRatio
}
if
(
typeof
height
!==
'
undefined
'
)
{
height
*=
pixelRatio
}
var
pageId
const
app
=
getApp
()
if
(
app
.
$route
&&
app
.
$route
.
params
.
__id__
)
{
...
...
@@ -838,23 +850,37 @@ export function canvasToTempFilePath ({
})
return
}
var
c
anvas
=
getTempCanvas
()
canvas
.
width
=
data
.
width
canvas
.
height
=
data
.
height
var
c
Width
=
(
typeof
destWidth
!==
'
undefined
'
)
?
(
destWidth
*
pixelRatio
)
:
data
.
width
var
cHeight
=
(
typeof
destHeight
!==
'
undefined
'
)
?
(
destHeight
*
pixelRatio
)
:
data
.
height
var
canvas
=
getTempCanvas
(
cWidth
,
cHeight
)
var
c2d
=
canvas
.
getContext
(
'
2d
'
)
c2d
.
putImageData
(
imgData
,
0
,
0
,
0
,
0
,
destWidth
||
imgData
.
width
,
destHeight
||
imgData
.
height
)
var
imageType
=
fileType
?
fileType
.
toLowerCase
()
:
'
png
'
var
base64
if
(
imageType
===
'
jpg
'
||
imageType
===
'
jpeg
'
)
{
var
tmpCanvas
=
canvas
.
cloneNode
(
true
)
var
tmpCtx
=
tmpCanvas
.
getContext
(
'
2d
'
)
tmpCtx
.
fillStyle
=
'
#fff
'
tmpCtx
.
fillRect
(
0
,
0
,
tmpCanvas
.
width
,
tmpCanvas
.
height
)
tmpCtx
.
drawImage
(
canvas
,
0
,
0
)
base64
=
tmpCanvas
.
toDataURL
(
`image/jpeg`
,
qualit
)
c2d
.
putImageData
(
imgData
,
0
,
0
,
0
,
0
,
imgData
.
width
,
imgData
.
height
)
var
imageType
=
(
fileType
?
fileType
.
toLowerCase
()
:
'
png
'
)
if
(
imageType
===
'
jpg
'
)
{
imageType
=
'
jpeg
'
}
var
dCanvas
var
isDest
=
(
typeof
destWidth
!==
'
undefined
'
&&
typeof
destHeight
!==
'
undefined
'
)
if
(
isDest
)
{
dCanvas
=
document
.
createElement
(
'
canvas
'
)
dCanvas
.
width
=
destWidth
dCanvas
.
height
=
destHeight
}
else
{
dCanvas
=
canvas
.
cloneNode
(
true
)
}
var
dCtx
=
dCanvas
.
getContext
(
'
2d
'
)
if
(
imageType
===
'
jpeg
'
)
{
dCtx
.
fillStyle
=
'
#fff
'
dCtx
.
fillRect
(
0
,
0
,
dCanvas
.
width
,
dCanvas
.
height
)
}
if
(
isDest
)
{
dCtx
.
drawImageByCanvas
(
canvas
,
0
,
0
,
canvas
.
width
,
canvas
.
height
,
0
,
0
,
destWidth
,
destHeight
,
true
)
}
else
{
base64
=
canvas
.
toDataURL
(
`image/
${
imageType
}
`
,
qualit
)
dCtx
.
drawImage
(
canvas
,
0
,
0
)
}
var
base64
=
dCanvas
.
toDataURL
(
`image/
${
imageType
}
`
,
qualit
)
invoke
(
callbackId
,
{
errMsg
:
'
canvasToTempFilePath:ok
'
,
...
...
@@ -866,8 +892,6 @@ export function canvasToTempFilePath ({
// img.onload = function () {
// canvas.width = destWidth || imgData.width
// canvas.height = destHeight || imgData.height
// c2d.fillStyle = '#fff'
// c2d.fillRect(0, 0, canvas.width, canvas.height)
// c2d.drawImage(img, 0, 0)
// base64 = canvas.toDataURL(`image/jpeg`, qualit)
// invoke(callbackId, {
...
...
src/core/view/components/canvas/index.vue
浏览文件 @
c9a2c894
...
...
@@ -10,6 +10,9 @@
<div
style=
"position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden;"
>
<slot
/>
</div>
<v-uni-resize-sensor
ref=
"sensor"
@
resize=
"_resize"
/>
</uni-canvas>
</
template
>
<
script
>
...
...
@@ -89,8 +92,8 @@ export default {
},
mounted
()
{
this
.
_resize
({
width
:
this
.
$
attrs
.
width
?
parseInt
(
this
.
$attrs
.
width
)
:
this
.
$el
.
offsetWidth
,
height
:
this
.
$
attrs
.
height
?
parseInt
(
this
.
$attrs
.
height
)
:
this
.
$el
.
offsetHeight
width
:
this
.
$
refs
.
sensor
.
$el
.
offsetWidth
,
height
:
this
.
$
refs
.
sensor
.
$el
.
offsetHeight
})
},
methods
:
{
...
...
@@ -108,11 +111,11 @@ export default {
height
})
{
var
canvas
=
this
.
$refs
.
canvas
if
(
canvas
.
width
!==
width
||
canvas
.
height
!==
height
)
{
if
(
canvas
.
style
.
width
!==
(
width
+
'
px
'
)
||
canvas
.
style
.
height
!==
(
height
+
'
px
'
)
)
{
canvas
.
width
=
width
canvas
.
height
=
height
wrapper
(
canvas
)
}
wrapper
(
canvas
)
},
_touchmove
(
event
)
{
event
.
preventDefault
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录