Skip to content

Commit

Permalink
Merge pull request #38 from toFrankie/master
Browse files Browse the repository at this point in the history
新增 wechat-svg 模板
  • Loading branch information
joeyguo authored Dec 7, 2023
2 parents 40afaae + 8048916 commit 724f86e
Show file tree
Hide file tree
Showing 59 changed files with 431 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
.svn
.DS_Store
.vscode
npm-debug.log
TODO.md
debug
Expand Down
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ node_modules
docs
.svn
.git
.vscode
.DS_Store
npm-debug.log
TODO.md
test
debug
package-lock.json
package-lock.json
5 changes: 2 additions & 3 deletions bin/gka.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function run(argv, dir, template) {
diff: argv.diff,
// bgcolor
bgcolor: argv.bgcolor,
// m
// m
mini: argv.mini,
// p
prefix: argv.prefix,
Expand All @@ -173,7 +173,7 @@ function run(argv, dir, template) {
});
}

const localTpls = ['css', 'canvas', 'svg'];
const localTpls = ['css', 'canvas', 'svg', 'wechat-svg'];

if (template === '') {
let tplList = tpl();
Expand Down Expand Up @@ -207,4 +207,3 @@ if (template === '') {
}
}
}

5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ gka 图片目录 -t 模板名
- 输出 svg 动画文件,支持 `自适应缩放雪碧图`
- 结合 -ucs 支持 `相同帧图片复用``空白裁剪优化``合图优化`✓ (可选) 

- wechat-svg

- 输出微信公众号文章所支持的 svg 序列帧片段
- 结合 -us 支持 `相同帧图片复用``合图优化`✓ (可选) 

#### 内置的自定义模板列表

- percent
Expand Down
5 changes: 5 additions & 0 deletions docs/zh-cn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ gka 图片目录 -t 模板名
- 输出 svg 动画文件,支持 `自适应缩放雪碧图`
- 结合 -ucs 支持 `相同帧图片复用``空白裁剪优化``合图优化`✓ (可选) 

- wechat-svg

- 输出微信公众号文章所支持的 svg 序列帧片段
- 结合 -us 支持 `相同帧图片复用``合图优化`✓ (可选) 

#### 内置的自定义模板列表

- percent
Expand Down
11 changes: 7 additions & 4 deletions lib/core/PluginApply.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const ImageNamePlugin = require("../plugins/ImageNamePlugin");

const CssTemplatePlugin = require("../templates/CssTemplatePlugin");
const SvgTemplatePlugin = require("../templates/SvgTemplatePlugin");
const WechatSvgTemplatePlugin = require('../templates/WechatSvgTemplatePlugin');
const CanvasTemplatePlugin = require("../templates/CanvasTemplatePlugin");

const ImageMinifyPlugin = require("../plugins/ImageMinifyPlugin");
Expand All @@ -28,9 +29,9 @@ const DataInfoPlugin = require("../plugins/DataInfoPlugin");
const LogOptionsPlugin = require("../plugins/LogOptionsPlugin");

class PluginApply {

process(options, compiler) {

new LogOptionsPlugin().apply(compiler);

new ImageRatioPlugin().apply(compiler);
Expand All @@ -46,12 +47,14 @@ class PluginApply {
new CanvasTemplatePlugin().apply(compiler);
else if (template === 'svg')
new SvgTemplatePlugin().apply(compiler);
else if (template === 'wechat-svg')
new WechatSvgTemplatePlugin().apply(compiler);
else if(_.isFunction(template)) // function template
template(compiler);
else if(fs.existsSync(template)) {
/**
* path template
*
*
* local \ node_modules \ global node_modules
*/
let PathTemplatePlugin = require(template);
Expand Down Expand Up @@ -82,7 +85,7 @@ class PluginApply {
new ImageMinifyPlugin().apply(compiler);
if (options.info)
new DataInfoPlugin().apply(compiler);

new ImageNamePlugin({
prefix: options.prefix,
dest: options.output,
Expand Down
Empty file modified lib/gka.js
100644 → 100755
Empty file.
182 changes: 182 additions & 0 deletions lib/templates/WechatSvgTemplatePlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* GKA (generate keyframes animation)
* author: joeyguo
* HomePage: https://github.com/joeyguo/gka
* MIT Licensed.
*
* gka wechat-svg template (Writtien by Frankie)
*/

const gkaUtils = require('../packages/gka-utils')

module.exports = class SvgTemplatePlugin {
constructor() {}
apply(compiler) {
compiler.hooks.on('templateOptions', (context, next) => {
// wechat-svg template do not support split/diff/crop/ratio/minirate
context.options.split = false
context.options.diff = false
context.options.crop = false
context.options.ratio = false
context.options.minirate = false

// 只支持 top-down 模式,left-right 模式有可能导致图片上传公众号后被压缩
if (context.options.sprites) context.options.algorithm = 'top-down'

next(context)
})

compiler.hooks.on('template', (context, next) => {
const { options, data } = context

function run(data, opts, key) {
const name = (key ? key + '-' : '') + 'gka'
const htmlName = name + '.html'
context.assets[htmlName] = html(data, opts)
}

run(data[0], options)

// 对每个子目录都进行处理
gkaUtils._.effectSubFolderSync(run, data[0], options)

next(context)
})
}
}

function html(data, opts) {
const width = data.frames[0].sourceW || data.frames[0].width
const height = data.frames[0].sourceH || data.frames[0].height
const html = gkaUtils.html.getHtmlWrap(opts)

html.includeBodyContent(`
${gkaUtils.html.getChangeWidthHTML(' ', width)}
<svg id="container" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" style="width: ${width}px">
${opts.sprites ? createAnimateTransformBySprites(data, opts) : createAnimateTransform(data, opts)}
</svg>`)

return html + ''
}

function createAnimateTransform(data, opts) {
const { frames } = data
const { frameduration } = opts

const duration = (frameduration * frames.length).toFixed(6)
const repeatIndexs = []
let prevKeyTime = 0 // 作为下一个的起点,以适配 --unique true 的情况

const getValues = (frames, index) => {
if (index === 0) return '1; 0; 0'
if (index === frames.length - 1) return '0; 1; 0'
return '0; 1; 0; 0'
}

const getKeyTimes = (frames, index) => {
const keyTime = ((index * frameduration) / duration).toFixed(6)
const nextKeyTime = (((index + 1) * frameduration) / duration).toFixed(6)

if (index === 0) {
prevKeyTime = nextKeyTime
return `0; ${nextKeyTime}; 1`
}

if (index === frames.length - 1) {
prevKeyTime = nextKeyTime
return `0; ${keyTime}; 1`
}

let keyTimes = `0; ${prevKeyTime}; ${nextKeyTime}; 1`
prevKeyTime = nextKeyTime
return keyTimes
}

const foreignObjectList = frames
.map((frame, index) => {
if (index > 0 && frame.file === frames[index - 1].file) repeatIndexs.push(index)
return frame
})
.map((frame, index) => {
if (repeatIndexs.includes(index)) return ''

const width = frame.sourceW || frame.width
const height = frame.sourceH || frame.height
const imgPath = './img/' + frame.file

const values = getValues(frames, index)
const keyTimes = getKeyTimes(frames, index)

return `
<foreignObject x="0" y="0" width="${width}" height="${height}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${height}" preserveAspectRatio="xMidYMin meet" style="width: 100%; opacity: 0; background-size: cover; background-position: left top; background-repeat: no-repeat; background-image: url(${imgPath})">
<animate attributeName="opacity" begin="0s" dur="${duration}s" values="${values}" keyTimes="${keyTimes}" calcMode="discrete" repeatCount="indefinite" fill="freeze" restart="always" />
</svg>
</foreignObject>`
})

return foreignObjectList.join('').trim()
}

function createAnimateTransformBySprites(data, opts) {
const { frames } = data
const { frameduration } = opts

const width = frames[0].sourceW || frames[0].width
const duration = (frameduration * frames.length).toFixed(6)
const imgName = gkaUtils.data.getImageNames(data)[0]
const imgPath = './img/' + imgName
const spriteImgHeight = frames[0].h

const repeatIndexs = []

const values = frames
.map((frame, index) => {
if (index === 0) return '0 0'
const value = `0 -${frame.y}`
if (index === frames.length - 1) return [value, value]
return value
})
.flat()
.filter((value, index, arr) => {
if (index === 0 || index === arr.length - 1) return true
if (value === arr[index - 1]) {
// 针对启用 --unique 时过滤重复项
repeatIndexs.push(index)
return false
}
return true
})
.join('; ')

const keyTimes = frames
.map((_, index) => {
const keyTime = ((index * frameduration) / duration).toFixed(6)
if (index === frames.length - 1) return [keyTime, 1]
return keyTime
})
.flat()
.filter((_, index) => !repeatIndexs.includes(index))
.join('; ')

// prettier-ignore
return `
<g transform="translate(0 0)">
<animateTransform
attributeName="transform"
type="translate"
begin="0s"
dur="${duration}s"
values="${values}"
keyTimes="${keyTimes}"
calcMode="discrete"
repeatCount="indefinite"
fill="freeze"
restart="never"
/>
<foreignObject x="0" y="0" width="${width}" height="${spriteImgHeight}">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${width} ${spriteImgHeight}" preserveAspectRatio="xMidYMin meet" style="width: 100%; background-size: cover; background-position: left top; background-repeat: no-repeat; background-image: url(${imgPath})"></svg>
</foreignObject>
</g>
`.trim();
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"imagemin-pngquant": "^5.0.0",
"imagemin-svgo": "^5.2.0",
"inquirer": "^3.2.0",
"js-beautify": "^1.7.5",
"js-beautify": "^1.14.11",
"lodash": "^4.17.5",
"md5": "^2.2.1",
"mkdirp": "^0.5.1",
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ gka 图片目录 -t 模板名
- 输出 svg 动画文件,支持 `自适应缩放雪碧图`
- 结合 -ucs 支持 `相同帧图片复用``空白裁剪优化``合图优化`✓ (可选) 

- wechat-svg

- 输出微信公众号文章所支持的 svg 序列帧片段
- 结合 -us 支持 `相同帧图片复用``合图优化`✓ (可选) 

#### 内置的自定义模板列表

- percent
Expand Down
8 changes: 8 additions & 0 deletions test/expected/gka-mulit-css/a-gka.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@
background-image: url("./img/a_a01.png");
background-size: 400px 300px;
}

11.11% {
background-image: url("./img/a_a02.png");
}

22.22% {
background-image: url("./img/a_a03.png");
}

33.33% {
background-image: url("./img/a_a04.png");
}

44.44% {
background-image: url("./img/a_a05.png");
}

55.56% {
background-image: url("./img/a_a06.png");
}

66.67% {
background-image: url("./img/a_a07.png");
}

77.78% {
background-image: url("./img/a_a08.png");
}

88.89%,
100% {
background-image: url("./img/a_a09.png");
Expand Down
10 changes: 10 additions & 0 deletions test/expected/gka-mulit-css/b-gka.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,43 @@
background-image: url("./img/b_a10.png");
background-size: 400px 300px;
}

9.09% {
background-image: url("./img/b_a11.png");
}

18.18% {
background-image: url("./img/b_a12.png");
}

27.27% {
background-image: url("./img/b_a13.png");
}

36.36% {
background-image: url("./img/b_a14.png");
}

45.45% {
background-image: url("./img/b_a15.png");
}

54.55% {
background-image: url("./img/b_a16.png");
}

63.64% {
background-image: url("./img/b_a17.png");
}

72.73% {
background-image: url("./img/b_a18.png");
}

81.82% {
background-image: url("./img/b_a19.png");
}

90.91%,
100% {
background-image: url("./img/b_a20.png");
Expand Down
Loading

0 comments on commit 724f86e

Please sign in to comment.