Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opacity骚操作 #21

Open
reng99 opened this issue May 12, 2019 · 0 comments
Open

opacity骚操作 #21

reng99 opened this issue May 12, 2019 · 0 comments
Labels
blog a single blog css 样式

Comments

@reng99
Copy link
Owner

reng99 commented May 12, 2019

origin_img

开篇如图,母亲节快乐@~@!

借着这个节日,结合下最近的需求:移动端h5生成图片没有二维码(如上),长按保存下来时候有二维码(如下)。我们来聊聊如何实现,文末配上不严谨的源码,感兴趣的看官自取啊~

handle_img

到这里,某些大佬应该明白怎么简单操作,完成这个尴尬的需求了,不过之前自己还是费了点功夫,脑袋转不过弯啊。

是的,结合opacity进行骚操作:

  1. 没有二维码的图片(原材料)展示出来给用户
  2. 生成的带二维码的图片覆盖在没有二维码图片上面
  3. 带有二维码的图片opacity置为0

嗯,既然要生成图片,且为了缓解后端的压力,我们前端来生成图片~

生成图片

generate_img

这里使用了html2canvas进行图片的生成操作。至于为什么选择html2canvas进行图片的生成呢?可以参考下富途的文章--移动端页面分享快照生成总结

使用html2canvas需要注意的地方有:

  • 使用图片img来代替背景图,截取的清晰度更好
  • 浏览器兼容
  • CSS样式不支持的情况
  • 图片如果存在跨域问题,这个必须设置好代理允许其跨域
  • etc

opacity操作

identify_code

接下来就是实现这个长按图片识别二维码的操作了,如上gif图。我长按了图片,给人的错觉就是按了那张没有带二维码的图片,实际上是按了带二维码的图片。因为两张图片的展示位置是一样的,带二维码的图片覆盖在最上面,并且其opacity设置为0了,所以长按的时候会出现识别图中二维码的字样。

就是这样啦,opacity结合absolute就可以轻松实现了😉

对了,gif图的二维码图片是使用qrcode生成的。

以上,如果还是不怎么了解,可以clone我的代码(见下)下来跑下就明白了~

备注

图片来源网络,侵删

源码:https://github.com/reng99/blogs/tree/master/src/opacity_demo

@reng99 reng99 added css 样式 blog a single blog and removed css 样式 labels May 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blog a single blog css 样式
Projects
None yet
Development

No branches or pull requests

1 participant