Skip to content

Commit

Permalink
cache bezier
Browse files Browse the repository at this point in the history
  • Loading branch information
fengruxian committed Dec 5, 2021
2 parents bd5abd5 + e9a9885 commit 141d140
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 32 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## 2.1.1 (2021-12-05)

### Features

* cache Bezier


## 2.0.0 (2021-07-19)
* Refactoring using WebGL.

Expand Down
2 changes: 1 addition & 1 deletion example/image-preview/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
hm.src = "https://hm.baidu.com/hm.js?e301069b9a5d63dcc7066a0a140f2195";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();</script><script defer="defer" src="test2.1.0.js"></script></head><h1>🌲image preview example</h1><h3>1.Generated by query selector</h3><h4>HTML:</h4><div class="code"><pre>
})();</script><script defer="defer" src="test2.1.1.js"></script></head><h1>🌲image preview example</h1><h3>1.Generated by query selector</h3><h4>HTML:</h4><div class="code"><pre>
&lt;div class="imageWraper"&gt;
&lt;img data-src="/testImage/main_body3.png" src="/testImage/main_body3.png"&gt;
&lt;img data-src="/testImage/phone20190624.png" src="/testImage/phone20190624.png"&gt;
Expand Down
1 change: 1 addition & 0 deletions example/image-preview/test2.1.1.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions example/js/image-preview-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ var imagePreviewModule = (function (exports) {

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -824,7 +825,10 @@ var imagePreviewModule = (function (exports) {
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1342,7 +1346,8 @@ var imagePreviewModule = (function (exports) {
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "image-preview",
"version": "2.1.0",
"version": "2.1.1",
"description": "image-preview",
"scripts": {
"start": "webpack serve --open Chrome.exe --mode=development",
"build": "node scripts/beforeBuild.js && npm run genModule",
"compileTs": "tsc --project ./src",
"buildTest": "webpack --mode=production",
"genModule": ".\\scripts\\genModule.cmd",
"genModule": "./scripts/genModule.sh",
"release": "standard-version"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion release/image-preview/image-preview-amd-min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions release/image-preview/image-preview-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ define(['exports'], function (exports) { 'use strict';

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -823,7 +824,10 @@ define(['exports'], function (exports) { 'use strict';
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1341,7 +1345,8 @@ define(['exports'], function (exports) { 'use strict';
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
2 changes: 1 addition & 1 deletion release/image-preview/image-preview-cjs-min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions release/image-preview/image-preview-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ var matrix = {

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -825,7 +826,10 @@ var cubicBezier = (function () {
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1343,7 +1347,8 @@ var webGl = (function () {
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
2 changes: 1 addition & 1 deletion release/image-preview/image-preview-esm-min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions release/image-preview/image-preview-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,7 @@ var matrix = {

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -821,7 +822,10 @@ var cubicBezier = (function () {
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1339,7 +1343,8 @@ var webGl = (function () {
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
2 changes: 1 addition & 1 deletion release/image-preview/image-preview-iife-min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions release/image-preview/image-preview-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@ var imagePreviewModule = (function (exports) {

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -824,7 +825,10 @@ var imagePreviewModule = (function (exports) {
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1342,7 +1346,8 @@ var imagePreviewModule = (function (exports) {
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
2 changes: 1 addition & 1 deletion release/image-preview/image-preview-umd-min.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions release/image-preview/image-preview-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@

var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -827,7 +828,10 @@
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down Expand Up @@ -1345,7 +1349,8 @@
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
6 changes: 5 additions & 1 deletion release/source/animation/animateJs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var cubicBezier = (function () {
function cubicBezier(x1, y1, x2, y2) {
this.cachedY = new Map();
this.precision = 1e-5;
this.p1 = {
x: x1,
Expand Down Expand Up @@ -61,7 +62,10 @@ var cubicBezier = (function () {
return t2;
};
cubicBezier.prototype.solve = function (x) {
return this.getY(this.solveCurveX(x));
if (!this.cachedY.get(x)) {
this.cachedY.set(x, this.getY(this.solveCurveX(x)));
}
return this.cachedY.get(x);
};
return cubicBezier;
}());
Expand Down
3 changes: 2 additions & 1 deletion release/source/webgl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ var webGl = (function () {
};
webGl.prototype.genPostion = function (width, height, index) {
var _a;
var z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
var zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
var z = -(this.viewHeight) / (zNearHeight) - forDev;
var viewWidth = this.viewWidth;
var sideZAxis = z - (viewWidth - width) / 2;
var positionsMap = [
Expand Down
4 changes: 0 additions & 4 deletions scripts/genModule.cmd

This file was deleted.

5 changes: 5 additions & 0 deletions scripts/genModule.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
cd scripts
node generateModule.js
cd ..
cp './release/image-preview/image-preview-iife.js' './example/js/'
4 changes: 2 additions & 2 deletions scripts/generateModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ try{
function compile(index,mini){
let mod = moduler[index];
let mininame = mini ? '-min': '';
let filename = `${releasePath}\\image-preview-${mod}${mininame}.js`;
let filename = `${releasePath}/image-preview-${mod}${mininame}.js`;
let miniParam = mini ? '--compact' : '';
childProcess.exec(`..\\node_modules\\.bin\\rollup -c -f ${mod} -o ${filename} ${miniParam}`,(err,stdout)=>{
childProcess.exec(`../node_modules/.bin/rollup -c -f ${mod} -o ${filename} ${miniParam}`,(err,stdout)=>{
if(err){
console.log(err)
console.log('编译失败')
Expand Down
6 changes: 5 additions & 1 deletion src/animation/animateJs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type coordinate = {
export class cubicBezier{
p1: coordinate
p2: coordinate
cachedY: Map<number,number> = new Map();
precision = 1e-5;
constructor(x1,y1,x2,y2){
this.p1 = {
Expand Down Expand Up @@ -89,7 +90,10 @@ export class cubicBezier{
return t2;
}
solve(x:number){
return this.getY( this.solveCurveX(x) )
if( !this.cachedY.get(x)){
this.cachedY.set(x,this.getY( this.solveCurveX(x) ))
}
return this.cachedY.get(x)
}
}
export var linear = new cubicBezier(0, 0, 1, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/core/image-preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* image-preview [2.1.0]
* image-preview [2.1.1]
* author:zilong
* https://github.com/daxiazilong
* Released under the MIT License
Expand Down
7 changes: 6 additions & 1 deletion src/webgl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,12 @@ class webGl {
})
}
genPostion(width: number, height: number,index:number) {
const z = -(this.viewHeight) / (2 * Math.tan(this.fieldOfViewInRadians / 2)) - forDev;
// zNearHeight = zNear * 2tan
// z zNear
// - = - => z = viewHeight / 2tan
// viewHeight zNearHeight
const zNearHeight = (2 * Math.tan(this.fieldOfViewInRadians / 2));
const z = -(this.viewHeight) / (zNearHeight) - forDev;
const viewWidth = this.viewWidth;

let sideZAxis = z - ( viewWidth - width ) / 2;
Expand Down

0 comments on commit 141d140

Please sign in to comment.