一个不到 1kb 的 image placeholder 的 js 库

image-placeholder.js

一个Javascript库,用于在浏览器端生成placeholder image,提供完善的配置和简单的方法。EN-Readmecss

Github:https://github.com/hustcc/placeholder.jsgit

预览

官方网站示例github

screenshots/website.png

screenshots/with_chinese.png

特色

  1. 不依赖任何 js 框架,好比 jqweb

  2. 小于 1k ,具体是0.98k,增长img配置方式以后,为1.52kbcanvas

  3. 配置项多,都有默认值浏览器

  4. 使用简单框架

使用

引入 placeholder.js 到你的前段代码中:dom

<script src="placeholder.js"></script>

1. 调用placeholder.js的方法进行替换,举个例子以下:字体

<img src="image_origin.png" onerror="this.src=placeholder.getData({text: 'Image 404'})">

2. 使用URL参数的方式配置Img的属性options,例如:网站

<img class="placeholder" />

或者携带参数options:

<img options="size=256x128&text=Hello!" class="placeholder" />

请注意img标签的class属性必须placeholder。

Placeholder 配置项

Placeholder 配置项用于API方法的输入参数, e.g. placeholder.getData({text: 'Image 404'}). 或者做为URL参数样式做为img的属性配置, e.g. options="size=256x128&text=Hello!"

  • size: placeholder图片尺寸. 例如: 256x128, 默认: 128x128.

  • bgcolor: 背景颜色. 例如: #969696. 默认: random.

  • color: 前景颜色,文字颜色. 例如: #ccc. 默认: random.

  • text: 自定义文本内容. 例如: Hello World, 你好. 默认: equal to size.

  • fstyle: 字体样式. 能够是 normal / italic / oblique. 默认: oblique.

  • fweight: 字体weight. 能够是 normal / bold / bolder / lighter / Number. 默认: bold.

  • fsize: 字体大小. 默认: 自动 计算字体大小防止文字超出图片大小.

  • ffamily: 字体. 默认: consolas.

具体参数配置例子:

var opts = {
      size: '512x256',
    bgcolor: '#ccc', 
    color: '#969696',
    text: 'Hello World, 你好',
    fstyle:'oblique',
    fweight: 'bold',
    fsize:'40',
    ffamily: 'consolas'
}
console.log(placeholder.getData(opts)) //get the base64 of the placeholder image.

做为img的options属性为:

size=256x128&text=Hello!&bgcolor=#ccc&color=#969696&fstyle=oblique&fweight=bold&fsize=40&ffamily=consolas

Placeholder 方法

  • placeholder.getData(opts): 得到placeholder图片的base64字符串,能够直接在img标签的src属性中使用,或者在css的背景中使用。

  • placeholder.getCanvas(opts): 得到canvas元素, 能够直接插入到DOM结构中。

为何要造轮子

  1. 不少 image placeholder 都是在服务端生成,须要额外搭建服务以及耗费资源。

  2. imsky/holder是客户端生成,大名鼎鼎,可是在用到网页上,动态图片的时候,它的那种风格用起来不太方便。

  3. 其余的 image placeholder 太大了,好比上面提到的 holder 压缩以后还有 30k 以上,其实不太使用在网页上使用

待完成

  1. 更多的灵活配置。

  2. 做为浏览器端的js库,尽量的压缩大小。

其余

任何问题欢迎反馈和 pr ,代码很是简单,相信 1 分钟你就能够看懂,很是欢迎push request

相关文章
相关标签/搜索