博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快应用-Canvas实现尺子左右滑动的效果
阅读量:3904 次
发布时间:2019-05-23

本文共 3661 字,大约阅读时间需要 12 分钟。

完成效果:

在这里插入图片描述
在web页面中,如果我们要实现左右滑动的效果,只需要在css中写上:
overflow: hidden;
overflow-x: scroll;
就可以达到效果。但是在快应用中,不支持overflow命令。

所以这里用canvas画图来实现:监听手指左右滑动的事件,手指每滑动一次,重新绘制一次尺子 从而给人视觉上滑动的效果。

核心代码:

template

{
{shortDate==today?'今天':shortDate.substr(0,4)+"年"+shortDate.substr(4,2)+"月"+shortDate.substr(6,2)+"日"}}
{
{foodInfo.FoodName}}
{
{foodInfo.Calory}}千卡/{
{foodInfo.Weight}}{
{foodInfo.UnitName}}
{
{(weight_initParams.count*(weight_initParams.minScale)).toFixed(1)}}
{
{activeUnitInfo.UnitName}}
约{
{((activeUnitInfo.Calory/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}千卡 {
{((activeUnitInfo.Weight/activeUnitInfo.Amount)*(weight_initParams.count*(weight_initParams.minScale))).toFixed(1)}}{
{foodInfo.UnitName}}
{
{$item.UnitName}}

Css:

.cover{
width:100%;height: 100%;background-color: #000000;position: fixed;top: 0px;left: 0px;opacity: 0.7}.Choice_count{
width: 100%;height:840px;position: fixed;bottom: 0;left: 0;background-color: #ffffff;flex-direction: column;border-top-left-radius:20px;border-top-right-radius:20px}.date_title{
width:100%;height:120px;border-bottom: 1px solid #EAE5DD;align-items: center;} .dateText{
font-size:36px;color: #473B1D;position:absolute;} .date{
width:100%;height: 120px;position: relative;align-items: center;justify-content: center;} .foodUnit{
width:140px;align-items: center;justify-content: center;} .foodUnit text{
lines:1;text-overflow:ellipsis;} .button{
background-color: #FFD359;width:100%;height: 105px;font-size: 32px;color: #473B1D;font-weight: bold;position:absolute;bottom: 0;left: 0;} .unitList{
align-items:center;justify-content: center;} .unit{
padding:10px,0;color:#473b1d;font-size: 32px;} .unitActive{
padding:10px,0;color:#ffd359;font-weight:bold;font-size: 32px;} .amount_module text{
color:#473b1d;font-weight:700} .amount_module{
width:100%;justify-content:center;align-items:flex-end;font-family:'Microsoft YaHei';} .rule_module{
width:100%;background-color:#fff;flex-direction:column;align-items: center;} .amount{
font-size:72px;font-weight:700;position:relative;top:12px;margin-right:6px} .ruler{
margin-top:6px} .new-canvas{
margin-top:20px;height:200px;width:1000px} .food_detail{
align-items: center;height:160px;justify-content:space-between;} .images{
height: 120px;;position: absolute;right:20px;padding:20px;align-items: center} .images image{
height:34px}

script:

代码中

img.src = ‘…/Common/ruler.png’ //加载本地图片
cxt.drawImage(img, (count * -24) + (i * 240), 0); //绘制图片
图片如下:

在这里插入图片描述

在刚开始实现尺子效果时,我并没有用到图片来画,是用canvas的画笔 line 线条来画的尺子,每滑动一次,就用线条绘制整个尺子效果,很耗性能,画面非常卡顿。后来再做优化的时候,尝试通过借助图片来画,可以流畅运行。

这里贴上的代码,是我从项目里摘出来实现尺子效果的核心代码,并不能直接运行。仅供一个实现思路和流程。

转载地址:http://bomen.baihongyu.com/

你可能感兴趣的文章
HDU 1559 最大子矩阵
查看>>
Open Judge 4010 :2011
查看>>
百练OJ-2815 城堡问题【DFS】
查看>>
CODE[VS] 1025 选菜 【背包】
查看>>
POJ 1724 ROADS【DFS+剪枝】
查看>>
AOJ 847 整数拆段
查看>>
AOJ 848 分数拆分
查看>>
UVA 133 The Dole Queue 【约瑟夫环】
查看>>
XDOJ 1208 B.笑爷买房 【DFS】
查看>>
部门年度工作总结的内容
查看>>
pandas学习笔记
查看>>
Numpy笔记
查看>>
正则表达式
查看>>
python线程进程笔记
查看>>
TensorFlow初学者必须了解的55个经典案例
查看>>
机器学习笔记
查看>>
数十种TensorFlow实现案例汇集:代码+笔记
查看>>
python记录的错误与知识
查看>>
内核中各种套接字的关系
查看>>
linux sysctl 参数实现 暨 ip_forward参数对Linux内核转发影响分析
查看>>