前言
没事的时候在看些canvas方面的文章,并写了一两个小demo。。下面的代码就是其中之一。感兴趣的先去看小姐姐的原文吧,从canvas基础讲起且非常详细,真的很不错o.o 小姐姐好像是饿了么的大神来着。。。仰望~~尊重原创 ;如果你只想要看代码,个人感觉自己写的要比小姐姐的原码清晰简洁点(最下面)。
效果图
说那麽多废话干嘛。。。有图就别哔哔哔
总结:(写在前面吧)
- 在使用缓存Canvas优化Star移动时,我的做法是在cache方法中创建缓存画布绘制完star将画布对象return出来,初衷是为了降低代码的复杂度,将代码模块化;但是好像动画更卡了(应该是和每次都创建画布有关…~~~~(>_<)~~~~),最后还是和原代码一样在全局上创建缓存画布(这样写动画效果很流畅,唉之后再想想如何简化代码写法吧^_^)。。
一点建议:(对自己说的)
- 在借鉴和学习别人的案例时,最好将整体效果或功能,划分成各个小块功能去逐一实现;对于某一小块功能的实现最好先自己思考如何实现(反正也不是让你一下想好实现全部功能,感觉原作者自己写的时候也不是一蹴而就的吧),功能实现后再和原代码相互印证(不同思想的碰撞bulabula。。。)学习大多数知识时也多是如此吧。
- 刚开始敲代码的时候,总是想着如何快速实现全部效果(干巴巴的,麻麻赖赖的,一点都不圆圆润,盘他~~~^_^),没有用自己掌握的canvas基础知识去想如何实现功能,反而在很多地方卡壳了(不是很关键的地方纠结)花费的时间更长了;有疑问的地方,可以先把疑问找个地方记下来(比如:’ 小本本 ‘。。。),等功能全部实现再回过头来解决掉’小本本’上记得内容。疑问: clientX、layerX、offsetX、pageX、screenX、x 的区别?。。。
- 之前一直觉得只要把产品方提的需求做出来就大吉大利今晚吃鸡了,即使是之前没做过的或者不懂的(不要紧,可以搬前辈大神们的嘛…),实际工作中遇到问题多是如此,这样功能或效果同样达到了要求(也仅仅是达到要求),但自己并没有规划如何开发,导致所写的代码真的是不忍直视(很za乱~~),因为多数有点东平西凑的嫌疑,感觉不是自己在驭使代码,更多的是个被驾驭的角色…希望自己在以后能多花点时间去规划如何去做,戒骄戒躁a le。。。
代码
1 |
|