Giter VIP home page Giter VIP logo

emoji's Introduction

Emoji.js

使用前端转换方式在不支持emoji表情符号的设备和浏览器中使用图片显示emoji表情符号,转换后的表情符号可以自适应文字大小(最大不超过20px,如果需要自定义的图片尺寸,请参见'重新生成代码和表情图片')。

每个emoji表情符号会被替换成对应的表情图片,在正常分辨率屏幕的设备上加载的图片尺寸为20×20,在高清屏上加载的图片尺寸为40×40。

为什么不使用CSS Sprites?

使用CSS Sprites虽然会减少请求数量,但是合并后图片过大,并且需要额外的css文件来定义sprites,
在大部分使用场景下,如果文本中的emoji数量有限的话使用单张图片替换是更好的方案。

Demo: http://coocy.github.io/emoji

安装:

拷贝emoji.jsemoji文件夹到你的项目中。

使用:

<html>
<head>
    <title>Title</title>
    <script type="text/javascript" src="emoji.js"></script>
</head>
<body>
    <div id="text">...</div>
    <script type="text/javascript">
        //转换包含emoji文本内容的DOM对象
        var el = document.getElementById('text');
        html = Emoji.emoji(el);
    </script>
</body>
</html>

如果页面使用了jQuery或者Zepto,可以这样调用:

$('#text').emoji();

重新生成代码和表情图片

需要安装Node.jsImageMagick

输出脚本output.js放在src文件夹中, 重新输出:

node src/output.js

output.js会执行以下动作:

  1. 从原始的emoji字典文件生成emoji.js中的替换正则表达式
  2. 生成index.html中的字符列表
  3. 使用原始emoji图片生成压缩后的图片,包括1x和2x两种尺寸

如果需要调整生成的emoji图片大小,修改output.js中的maxEmojiSize变量值,然后执行输出脚本

CREDITS

参考了php-emoji项目: https://github.com/iamcal/php-emoji

emoji字典和图片来自emoji-data项目: https://github.com/iamcal/emoji-data

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.