mirror of
https://github.com/lrsjng/pagemap.git
synced 2025-09-14 23:02:08 +02:00
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
294813ff26 | ||
|
341bf3e960 | ||
|
84118a0463 | ||
|
407c6617db | ||
|
8f15b44a13 | ||
|
f6b1e153a0 |
@@ -12,7 +12,7 @@ insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
|
||||
[{package.json,.eslintrc,.travis.yml}]
|
||||
[{*.json,*.yml,.eslintrc}]
|
||||
indent_size = 2
|
||||
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
build
|
||||
coverage
|
||||
dist
|
||||
es5
|
||||
local
|
||||
node_modules
|
||||
/build/
|
||||
/coverage/
|
||||
/dist/
|
||||
/es5/
|
||||
/local/
|
||||
/node_modules/
|
||||
|
@@ -5,9 +5,6 @@
|
||||
es6: true
|
||||
node: true
|
||||
|
||||
ecmaFeatures:
|
||||
ecmaVersion: 6
|
||||
|
||||
rules:
|
||||
array-bracket-spacing: [2, never]
|
||||
arrow-parens: [2, as-needed]
|
||||
|
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1,5 +1,5 @@
|
||||
build
|
||||
coverage
|
||||
local
|
||||
node_modules
|
||||
npm-debug.log
|
||||
/build/
|
||||
/coverage/
|
||||
/local/
|
||||
/node_modules/
|
||||
/npm-debug.log
|
||||
|
@@ -1,7 +1,7 @@
|
||||
# pagemap
|
||||
|
||||
[![license][license-img]][github] [![web][web-img]][web] [![github][github-img]][github] [![npm][npm-img]][npm]
|
||||
[![version][npm-v-img]][npm] [![downloads][npm-dm-img]][npm] [![dependencies status][gemnasium-img]][gemnasium]
|
||||
[![version][npm-v-img]][npm] [![downloads][npm-dm-img]][npm]
|
||||
|
||||
Mini map for web pages.
|
||||
|
||||
@@ -10,7 +10,7 @@ Mini map for web pages.
|
||||
|
||||
add a `canvas` tag to your HTML page:
|
||||
```html
|
||||
<canvas id='#map'></canvas>
|
||||
<canvas id='map'></canvas>
|
||||
```
|
||||
|
||||
fix it's position on the screen:
|
||||
@@ -45,7 +45,7 @@ pagemap(document.querySelector('#map'), {
|
||||
## License
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Lars Jung (https://larsjung.de)
|
||||
Copyright (c) 2019 Lars Jung (https://larsjung.de)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -69,7 +69,6 @@ THE SOFTWARE.
|
||||
[web]: https://larsjung.de/pagemap/
|
||||
[github]: https://github.com/lrsjng/pagemap
|
||||
[npm]: https://www.npmjs.org/package/pagemap
|
||||
[gemnasium]: https://gemnasium.com/lrsjng/pagemap
|
||||
|
||||
[license-img]: https://img.shields.io/badge/license-MIT-a0a060.svg?style=flat-square
|
||||
[web-img]: https://img.shields.io/badge/web-larsjung.de/pagemap-a0a060.svg?style=flat-square
|
||||
@@ -78,4 +77,3 @@ THE SOFTWARE.
|
||||
|
||||
[npm-v-img]: https://img.shields.io/npm/v/pagemap.svg?style=flat-square
|
||||
[npm-dm-img]: https://img.shields.io/npm/dm/pagemap.svg?style=flat-square
|
||||
[gemnasium-img]: https://img.shields.io/gemnasium/lrsjng/pagemap.svg?style=flat-square
|
||||
|
@@ -1,84 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>pagemap boxes demo</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
body {
|
||||
width: 2000px;
|
||||
height: 2000px;
|
||||
background-color: #eee;
|
||||
}
|
||||
#map {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
width: 120px;
|
||||
height: 100%;
|
||||
z-index: 100;
|
||||
}
|
||||
#container {
|
||||
position: relative;
|
||||
left: 100px;
|
||||
top: 100px;
|
||||
width: 600px;
|
||||
height: 600px;
|
||||
outline: 4px solid #222;
|
||||
overflow: auto;
|
||||
}
|
||||
#content {
|
||||
width: 800px;
|
||||
height: 1000px;
|
||||
background-color: #ccc;
|
||||
}
|
||||
#container2 {
|
||||
position: relative;
|
||||
left: 100px;
|
||||
top: 100px;
|
||||
width: 300px;
|
||||
height: 400px;
|
||||
outline: 4px solid #222;
|
||||
overflow: auto;
|
||||
}
|
||||
#content2 {
|
||||
width: 500px;
|
||||
height: 600px;
|
||||
background-color: #aaa;
|
||||
}
|
||||
.checkers {
|
||||
background-image:
|
||||
linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff),
|
||||
linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff);
|
||||
background-size: 100px 100px;
|
||||
background-position: 0 0, 50px 50px
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body class="checkers">
|
||||
<div id="container">
|
||||
<div id="content" class="checkers">
|
||||
<div id="container2">
|
||||
<div id="content2" class="checkers"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<canvas id="map"></canvas>
|
||||
|
||||
<script src="../dist/pagemap.js"></script>
|
||||
<script>
|
||||
pagemap(document.querySelector('#map'), {
|
||||
// viewport: document.querySelector('#container'),
|
||||
interval: 50,
|
||||
styles: {
|
||||
div: 'rgba(0,0,0,0.1)'
|
||||
},
|
||||
back: 'rgba(255,255,255,1)'
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
2
dist/pagemap.js
vendored
2
dist/pagemap.js
vendored
@@ -1,2 +0,0 @@
|
||||
/*! pagemap v0.1.0 - https://larsjung.de/pagemap/ */
|
||||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.pagemap=e():t.pagemap=e()}(this,function(){return function(t){function e(n){if(o[n])return o[n].exports;var r=o[n]={exports:{},id:n,loaded:!1};return t[n].call(r.exports,r,r.exports,e),r.loaded=!0,r.exports}var o={};return e.m=t,e.c=o,e.p="",e(0)}([function(t,e,o){(function(e){"use strict";var n=o(1),r=e.window,i=r.document,c=i.querySelector("body"),u=function(t){return"rgba(0,0,0,"+t/100+")"},f={viewport:null,styles:{"header,footer,section,article":u(8),"h1,a":u(10),"h2,h3,h4":u(8)},back:u(2),view:u(5),drag:u(10),interval:null},l=function(t,e,o,n){return o.split(/\s+/).forEach(function(o){return t[e](o,n)})},s=function(t,e,o){return l(t,"addEventListener",e,o)},a=function(t,e,o){return l(t,"removeEventListener",e,o)};t.exports=function(t,e){var o=Object.assign({},f,e),u=t.getContext("2d"),l=function(){var e=t.clientWidth,o=t.clientHeight;return function(t,n){return Math.min(e/t,o/n)}}(),p=function(e,o){t.width=e,t.height=o,t.style.width=e+"px",t.style.height=o+"px"},h=o.viewport,d=function(t){return Array.from((h||i).querySelectorAll(t))},v=!1,y=void 0,g=void 0,w=void 0,x=void 0,m=void 0,b=function(t,e){e&&(u.beginPath(),u.rect(t.x,t.y,t.w,t.h),u.fillStyle=e,u.fill())},T=function(t){Object.keys(t).forEach(function(e){var o=t[e];d(e).forEach(function(t){b(n.ofElement(t).relativeTo(y),o)})})},E=function(){y=h?n.ofContent(h):n.ofDocument(),g=h?n.ofViewport(h):n.ofWindow(),w=l(y.w,y.h),p(y.w*w,y.h*w),u.setTransform(1,0,0,1,0,0),u.clearRect(0,0,t.width,t.height),u.scale(w,w),b(y.relativeTo(y),o.back),T(o.styles),b(g.relativeTo(y),v?o.drag:o.view)},O=function(e){e.preventDefault();var o=n.ofViewport(t),i=(e.pageX-o.x)/w-g.w*x,c=(e.pageY-o.y)/w-g.h*m;h?(h.scrollLeft=i,h.scrollTop=c):r.scrollTo(i,c),E()},W=function L(e){v=!1,t.style.cursor="pointer",c.style.cursor="auto",a(r,"mousemove",O),a(r,"mouseup",L),O(e)},j=function(e){v=!0;var o=n.ofViewport(t),i=g.relativeTo(y);x=((e.pageX-o.x)/w-i.x)/i.w,m=((e.pageY-o.y)/w-i.y)/i.h,(0>x||x>1||0>m||m>1)&&(x=.5,m=.5),t.style.cursor="crosshair",c.style.cursor="crosshair",s(r,"mousemove",O),s(r,"mouseup",W),O(e)},H=function(){t.style.cursor="pointer",s(t,"mousedown",j),s(h||r,"load resize scroll",E),o.interval>0&&setInterval(function(){return E()},o.interval),E()};return H(),{redraw:E}}}).call(e,function(){return this}())},function(t,e){(function(e){"use strict";var o=e.window,n=o.document.documentElement,r=t.exports=function(t,e,o,n){return Object.assign(Object.create(r.prototype),{x:t,y:e,w:o,h:n})};r.prototype={constructor:r,relativeTo:function(){var t=arguments.length<=0||void 0===arguments[0]?{x:0,y:0}:arguments[0];return r(this.x-t.x,this.y-t.y,this.w,this.h)}},r.ofDocument=function(){return r(0,0,n.scrollWidth,n.scrollHeight)},r.ofWindow=function(){return r(o.pageXOffset,o.pageYOffset,n.clientWidth,n.clientHeight)};var i=function(t){var e=t.getBoundingClientRect();return{x:e.left+o.pageXOffset,y:e.top+o.pageYOffset}};r.ofElement=function(t){var e=i(t),o=e.x,n=e.y;return r(o,n,t.offsetWidth,t.offsetHeight)},r.ofViewport=function(t){var e=i(t),o=e.x,n=e.y;return r(o+t.clientLeft,n+t.clientTop,t.clientWidth,t.clientHeight)},r.ofContent=function(t){var e=i(t),o=e.x,n=e.y;return r(o+t.clientLeft-t.scrollLeft,n+t.clientTop-t.scrollTop,t.scrollWidth,t.scrollHeight)}}).call(e,function(){return this}())}])});
|
2
dist/pagemap.min.js
vendored
Normal file
2
dist/pagemap.min.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/*! pagemap v0.5.0 - https://larsjung.de/pagemap/ */
|
||||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.pagemap=e():t.pagemap=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){(function(e){"use strict";var n=e.window,r=n.document,o=r.documentElement,i=r.querySelector("body"),c=function(t){return"rgba(0,0,0,"+t/100+")"},u={viewport:null,styles:{"header,footer,section,article":c(8),"h1,a":c(10),"h2,h3,h4":c(8)},back:c(2),view:c(5),drag:c(10),interval:null},l=function(t,e,n,r){return n.split(/\s+/).forEach(function(n){return t[e](n,r)})},f=function(t,e,n){return l(t,"addEventListener",e,n)},s=function(t,e,n){return l(t,"removeEventListener",e,n)},a=function(t,e,n,r){return{x:t,y:e,w:n,h:r}},h=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{x:0,y:0};return a(t.x-e.x,t.y-e.y,t.w,t.h)},p=function(){return a(0,0,o.scrollWidth,o.scrollHeight)},d=function(){return a(n.pageXOffset,n.pageYOffset,o.clientWidth,o.clientHeight)},v=function(t){var e=t.getBoundingClientRect();return{x:e.left+n.pageXOffset,y:e.top+n.pageYOffset}},y=function(t){var e=v(t),n=e.x,r=e.y;return a(n,r,t.offsetWidth,t.offsetHeight)},g=function(t){var e=v(t),n=e.x,r=e.y;return a(n+t.clientLeft,r+t.clientTop,t.clientWidth,t.clientHeight)},x=function(t){var e=v(t),n=e.x,r=e.y;return a(n+t.clientLeft-t.scrollLeft,r+t.clientTop-t.scrollTop,t.scrollWidth,t.scrollHeight)};t.exports=function(t,e){var o=Object.assign({},u,e),c=t.getContext("2d"),l=function(){var e=t.clientWidth,n=t.clientHeight;return function(t,r){return Math.min(e/t,n/r)}}(),a=function(e,n){t.width=e,t.height=n,t.style.width=e+"px",t.style.height=n+"px"},v=o.viewport,m=function(t){return Array.from((v||r).querySelectorAll(t))},w=!1,b=void 0,E=void 0,H=void 0,L=void 0,O=void 0,T=function(t,e){e&&(c.beginPath(),c.rect(t.x,t.y,t.w,t.h),c.fillStyle=e,c.fill())},W=function(t){Object.keys(t).forEach(function(e){var n=t[e];m(e).forEach(function(t){T(h(y(t),b),n)})})},j=function(){b=v?x(v):p(),E=v?g(v):d(),H=l(b.w,b.h),a(b.w*H,b.h*H),c.setTransform(1,0,0,1,0,0),c.clearRect(0,0,t.width,t.height),c.scale(H,H),T(h(b,b),o.back),W(o.styles),T(h(E,b),w?o.drag:o.view)},X=function(e){e.preventDefault();var r=g(t),o=(e.pageX-r.x)/H-E.w*L,i=(e.pageY-r.y)/H-E.h*O;v?(v.scrollLeft=o,v.scrollTop=i):n.scrollTo(o,i),j()},Y=function e(r){w=!1,t.style.cursor="pointer",i.style.cursor="auto",s(n,"mousemove",X),s(n,"mouseup",e),X(r)},k=function(e){w=!0;var r=g(t),o=h(E,b);L=((e.pageX-r.x)/H-o.x)/o.w,O=((e.pageY-r.y)/H-o.y)/o.h,(L<0||L>1||O<0||O>1)&&(L=.5,O=.5),t.style.cursor="crosshair",i.style.cursor="crosshair",f(n,"mousemove",X),f(n,"mouseup",Y),X(e)},S=function(){t.style.cursor="pointer",f(t,"mousedown",k),f(v||n,"load resize scroll",j),o.interval>0&&setInterval(function(){return j()},o.interval),j()};return S(),{redraw:j}}}).call(e,function(){return this}())}])});
|
80
ghu.js
Normal file
80
ghu.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const {resolve, join} = require('path');
|
||||
const {ghu, jszip, mapfn, read, remove, run, uglify, webpack, wrap, write} = require('ghu');
|
||||
|
||||
const NAME = 'pagemap';
|
||||
|
||||
const ROOT = resolve(__dirname);
|
||||
const SRC = join(ROOT, 'src');
|
||||
const DEMO = join(SRC, 'demo');
|
||||
const BUILD = join(ROOT, 'build');
|
||||
const DIST = join(ROOT, 'dist');
|
||||
|
||||
ghu.defaults('release');
|
||||
|
||||
ghu.before(runtime => {
|
||||
runtime.pkg = Object.assign({}, require('./package.json'));
|
||||
runtime.comment = `${NAME} v${runtime.pkg.version} - ${runtime.pkg.homepage}`;
|
||||
runtime.commentJs = `/*! ${runtime.comment} */\n`;
|
||||
console.log(runtime.comment);
|
||||
});
|
||||
|
||||
ghu.task('clean', () => {
|
||||
return remove(`${BUILD}, ${DIST}`);
|
||||
});
|
||||
|
||||
ghu.task('lint', () => {
|
||||
return run('eslint .', {stdio: 'inherit'});
|
||||
});
|
||||
|
||||
ghu.task('build:script', runtime => {
|
||||
const webpackConfig = {
|
||||
output: {
|
||||
library: NAME,
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
module: {
|
||||
loaders: [
|
||||
{
|
||||
include: [SRC],
|
||||
loader: 'babel-loader',
|
||||
query: {
|
||||
cacheDirectory: true,
|
||||
presets: ['env']
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
return read(`${SRC}/${NAME}.js`)
|
||||
.then(webpack(webpackConfig, {showStats: false}))
|
||||
.then(uglify({compressor: {warnings: false}}))
|
||||
.then(wrap(runtime.commentJs))
|
||||
.then(write(`${DIST}/${NAME}.min.js`, {overwrite: true}))
|
||||
.then(write(`${BUILD}/${NAME}-${runtime.pkg.version}.min.js`, {overwrite: true}));
|
||||
});
|
||||
|
||||
ghu.task('build:copy', () => {
|
||||
return read(`${ROOT}/*.md`)
|
||||
.then(write(mapfn.p(ROOT, BUILD), {overwrite: true}));
|
||||
});
|
||||
|
||||
ghu.task('build:demo', ['build:script'], () => {
|
||||
return Promise.all([
|
||||
read(`${DEMO}: *`)
|
||||
.then(write(mapfn.p(SRC, BUILD), {overwrite: true})),
|
||||
|
||||
read(`${BUILD}: ${NAME}-*.min.js`)
|
||||
.then(write(`${BUILD}/demo/${NAME}.min.js`, {overwrite: true}))
|
||||
]);
|
||||
});
|
||||
|
||||
ghu.task('build', ['build:script', 'build:copy', 'build:demo']);
|
||||
|
||||
ghu.task('zip', ['build'], runtime => {
|
||||
return read(`${BUILD}/**`)
|
||||
.then(jszip({dir: BUILD, level: 9}))
|
||||
.then(write(`${BUILD}/${NAME}-${runtime.pkg.version}.zip`, {overwrite: true}));
|
||||
});
|
||||
|
||||
ghu.task('release', ['clean', 'zip']);
|
148
lib/pagemap.js
148
lib/pagemap.js
@@ -1,148 +0,0 @@
|
||||
const Rect = require('./rect');
|
||||
|
||||
const win = global.window;
|
||||
const doc = win.document;
|
||||
const body = doc.querySelector('body');
|
||||
|
||||
const black = pc => `rgba(0,0,0,${pc / 100})`;
|
||||
const defaults = {
|
||||
viewport: null,
|
||||
styles: {
|
||||
'header,footer,section,article': black(8),
|
||||
'h1,a': black(10),
|
||||
'h2,h3,h4': black(8)
|
||||
},
|
||||
back: black(2),
|
||||
view: black(5),
|
||||
drag: black(10),
|
||||
interval: null
|
||||
};
|
||||
|
||||
const _listener = (el, method, types, fn) => types.split(/\s+/).forEach(type => el[method](type, fn));
|
||||
const on = (el, types, fn) => _listener(el, 'addEventListener', types, fn);
|
||||
const off = (el, types, fn) => _listener(el, 'removeEventListener', types, fn);
|
||||
|
||||
module.exports = (canvas, options) => {
|
||||
const settings = Object.assign({}, defaults, options);
|
||||
|
||||
const context = canvas.getContext('2d');
|
||||
|
||||
const calcScale = (() => {
|
||||
const width = canvas.clientWidth;
|
||||
const height = canvas.clientHeight;
|
||||
return (w, h) => Math.min(width / w, height / h);
|
||||
})();
|
||||
|
||||
const resizeCanvas = (w, h) => {
|
||||
canvas.width = w;
|
||||
canvas.height = h;
|
||||
canvas.style.width = `${w}px`;
|
||||
canvas.style.height = `${h}px`;
|
||||
};
|
||||
|
||||
const viewport = settings.viewport;
|
||||
const find = sel => Array.from((viewport || doc).querySelectorAll(sel));
|
||||
|
||||
let drag = false;
|
||||
|
||||
let rootRect;
|
||||
let viewRect;
|
||||
let scale;
|
||||
let dragRx;
|
||||
let dragRy;
|
||||
|
||||
const drawRect = (rect, col) => {
|
||||
if (!col) {
|
||||
return;
|
||||
}
|
||||
context.beginPath();
|
||||
context.rect(rect.x, rect.y, rect.w, rect.h);
|
||||
context.fillStyle = col;
|
||||
context.fill();
|
||||
};
|
||||
|
||||
const applyStyles = styles => {
|
||||
Object.keys(styles).forEach(sel => {
|
||||
const col = styles[sel];
|
||||
find(sel).forEach(el => {
|
||||
drawRect(Rect.ofElement(el).relativeTo(rootRect), col);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const draw = () => {
|
||||
rootRect = viewport ? Rect.ofContent(viewport) : Rect.ofDocument();
|
||||
viewRect = viewport ? Rect.ofViewport(viewport) : Rect.ofWindow();
|
||||
scale = calcScale(rootRect.w, rootRect.h);
|
||||
|
||||
resizeCanvas(rootRect.w * scale, rootRect.h * scale);
|
||||
|
||||
context.setTransform(1, 0, 0, 1, 0, 0);
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.scale(scale, scale);
|
||||
|
||||
drawRect(rootRect.relativeTo(rootRect), settings.back);
|
||||
applyStyles(settings.styles);
|
||||
drawRect(viewRect.relativeTo(rootRect), drag ? settings.drag : settings.view);
|
||||
};
|
||||
|
||||
const onDrag = ev => {
|
||||
ev.preventDefault();
|
||||
const cr = Rect.ofViewport(canvas);
|
||||
const x = (ev.pageX - cr.x) / scale - viewRect.w * dragRx;
|
||||
const y = (ev.pageY - cr.y) / scale - viewRect.h * dragRy;
|
||||
|
||||
if (viewport) {
|
||||
viewport.scrollLeft = x;
|
||||
viewport.scrollTop = y;
|
||||
} else {
|
||||
win.scrollTo(x, y);
|
||||
}
|
||||
draw();
|
||||
};
|
||||
|
||||
const onDragEnd = ev => {
|
||||
drag = false;
|
||||
|
||||
canvas.style.cursor = 'pointer';
|
||||
body.style.cursor = 'auto';
|
||||
off(win, 'mousemove', onDrag);
|
||||
off(win, 'mouseup', onDragEnd);
|
||||
onDrag(ev);
|
||||
};
|
||||
|
||||
const onDragStart = ev => {
|
||||
drag = true;
|
||||
|
||||
const cr = Rect.ofViewport(canvas);
|
||||
const vr = viewRect.relativeTo(rootRect);
|
||||
dragRx = ((ev.pageX - cr.x) / scale - vr.x) / vr.w;
|
||||
dragRy = ((ev.pageY - cr.y) / scale - vr.y) / vr.h;
|
||||
if (dragRx < 0 || dragRx > 1 || dragRy < 0 || dragRy > 1) {
|
||||
dragRx = 0.5;
|
||||
dragRy = 0.5;
|
||||
}
|
||||
|
||||
canvas.style.cursor = 'crosshair';
|
||||
body.style.cursor = 'crosshair';
|
||||
on(win, 'mousemove', onDrag);
|
||||
on(win, 'mouseup', onDragEnd);
|
||||
onDrag(ev);
|
||||
};
|
||||
|
||||
const init = () => {
|
||||
canvas.style.cursor = 'pointer';
|
||||
on(canvas, 'mousedown', onDragStart);
|
||||
on(viewport || win, 'load resize scroll', draw);
|
||||
if (settings.interval > 0) {
|
||||
setInterval(() => draw(), settings.interval);
|
||||
}
|
||||
draw();
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
return {
|
||||
redraw: draw
|
||||
};
|
||||
};
|
67
lib/rect.js
67
lib/rect.js
@@ -1,67 +0,0 @@
|
||||
const win = global.window;
|
||||
const docEl = win.document.documentElement;
|
||||
|
||||
const Rect = module.exports = (x, y, w, h) => {
|
||||
return Object.assign(Object.create(Rect.prototype), {
|
||||
x, y, w, h
|
||||
});
|
||||
};
|
||||
|
||||
Rect.prototype = {
|
||||
constructor: Rect,
|
||||
|
||||
relativeTo(pos = {x: 0, y: 0}) {
|
||||
return Rect(this.x - pos.x, this.y - pos.y, this.w, this.h);
|
||||
}
|
||||
};
|
||||
|
||||
Rect.ofDocument = () => {
|
||||
return Rect(0, 0, docEl.scrollWidth, docEl.scrollHeight);
|
||||
};
|
||||
|
||||
Rect.ofWindow = () => {
|
||||
return Rect(
|
||||
win.pageXOffset,
|
||||
win.pageYOffset,
|
||||
docEl.clientWidth,
|
||||
docEl.clientHeight
|
||||
);
|
||||
};
|
||||
|
||||
const getOffset = el => {
|
||||
const br = el.getBoundingClientRect();
|
||||
return {
|
||||
x: br.left + win.pageXOffset,
|
||||
y: br.top + win.pageYOffset
|
||||
};
|
||||
};
|
||||
|
||||
Rect.ofElement = el => {
|
||||
const {x, y} = getOffset(el);
|
||||
return Rect(
|
||||
x,
|
||||
y,
|
||||
el.offsetWidth,
|
||||
el.offsetHeight
|
||||
);
|
||||
};
|
||||
|
||||
Rect.ofViewport = el => {
|
||||
const {x, y} = getOffset(el);
|
||||
return Rect(
|
||||
x + el.clientLeft,
|
||||
y + el.clientTop,
|
||||
el.clientWidth,
|
||||
el.clientHeight
|
||||
);
|
||||
};
|
||||
|
||||
Rect.ofContent = el => {
|
||||
const {x, y} = getOffset(el);
|
||||
return Rect(
|
||||
x + el.clientLeft - el.scrollLeft,
|
||||
y + el.clientTop - el.scrollTop,
|
||||
el.scrollWidth,
|
||||
el.scrollHeight
|
||||
);
|
||||
};
|
6687
package-lock.json
generated
Normal file
6687
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
15
package.json
15
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pagemap",
|
||||
"version": "0.1.0",
|
||||
"version": "0.5.0",
|
||||
"description": "Mini map for web pages.",
|
||||
"homepage": "https://larsjung.de/pagemap/",
|
||||
"bugs": "https://github.com/lrsjng/pagemap/issues",
|
||||
@@ -13,17 +13,16 @@
|
||||
"main": "dist/pagemap",
|
||||
"scripts": {
|
||||
"lint": "eslint .",
|
||||
"build": "rimraf dist && webpack",
|
||||
"build": "node ghu release",
|
||||
"precommit": "npm run -s lint && npm run -s build"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-loader": "6.2.4",
|
||||
"babel-preset-es2015": "6.9.0",
|
||||
"eslint": "2.12.0",
|
||||
"rimraf": "2.5.2",
|
||||
"webpack": "1.13.1"
|
||||
"babel-loader": "7.0.0",
|
||||
"babel-preset-env": "1.7.0",
|
||||
"eslint": "5.15.1",
|
||||
"ghu": "0.13.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=6.0.0"
|
||||
"node": ">=8.0.0"
|
||||
}
|
||||
}
|
||||
|
59
src/demo/boxes.css
Normal file
59
src/demo/boxes.css
Normal file
@@ -0,0 +1,59 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
width: 2000px;
|
||||
height: 2000px;
|
||||
background-color: #eee;
|
||||
}
|
||||
|
||||
#map {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
width: 120px;
|
||||
height: 100%;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
#container {
|
||||
position: relative;
|
||||
left: 100px;
|
||||
top: 100px;
|
||||
width: 600px;
|
||||
height: 600px;
|
||||
outline: 4px solid #222;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
#content {
|
||||
width: 800px;
|
||||
height: 1000px;
|
||||
background-color: #ccc;
|
||||
}
|
||||
|
||||
#container2 {
|
||||
position: relative;
|
||||
left: 100px;
|
||||
top: 100px;
|
||||
width: 300px;
|
||||
height: 400px;
|
||||
outline: 4px solid #222;
|
||||
overflow: auto;
|
||||
}
|
||||
|
||||
#content2 {
|
||||
width: 500px;
|
||||
height: 600px;
|
||||
background-color: #aaa;
|
||||
}
|
||||
|
||||
.checkers {
|
||||
background-image:
|
||||
linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff),
|
||||
linear-gradient(45deg, #fff 25%, transparent 25%, transparent 75%, #fff 75%, #fff);
|
||||
background-size: 100px 100px;
|
||||
background-position: 0 0, 50px 50px
|
||||
}
|
31
src/demo/boxes.html
Normal file
31
src/demo/boxes.html
Normal file
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>pagemap boxes demo</title>
|
||||
<link rel="stylesheet" href="boxes.css">
|
||||
</head>
|
||||
<body class="checkers">
|
||||
<div id="container">
|
||||
<div id="content" class="checkers">
|
||||
<div id="container2">
|
||||
<div id="content2" class="checkers"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<canvas id="map"></canvas>
|
||||
|
||||
<script src="pagemap.min.js"></script>
|
||||
<script>
|
||||
pagemap(document.querySelector('#map'), {
|
||||
// viewport: document.querySelector('#container'),
|
||||
interval: 50,
|
||||
styles: {
|
||||
div: 'rgba(0,0,0,0.1)'
|
||||
},
|
||||
back: 'rgba(255,255,255,1)'
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
47
src/demo/text.css
Normal file
47
src/demo/text.css
Normal file
@@ -0,0 +1,47 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
#map {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
width: 200px;
|
||||
height: 100%;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: Bitter, Ubuntu, Arial, sans;
|
||||
font-size: 16px;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
main {
|
||||
margin: 3em auto 6em auto;
|
||||
max-width: 800px;
|
||||
line-height: 1.6em;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 3em;
|
||||
font-weight: normal;
|
||||
margin: 0 0 1em;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 1.8em;
|
||||
font-weight: normal;
|
||||
margin: 3em 0 1em;
|
||||
}
|
||||
|
||||
h3 {
|
||||
font-size: 1.2em;
|
||||
font-weight: bold;
|
||||
margin: 2em 0 1em;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #1E88E5;
|
||||
}
|
@@ -3,50 +3,16 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>pagemap text demo</title>
|
||||
<style>
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
#map {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
right: 0;
|
||||
width: 200px;
|
||||
height: 100%;
|
||||
z-index: 100;
|
||||
}
|
||||
body {
|
||||
font-family: Bitter, Ubuntu, Arial, sans;
|
||||
font-size: 16px;
|
||||
color: #555;
|
||||
}
|
||||
main {
|
||||
margin: 3em auto 6em auto;
|
||||
max-width: 800px;
|
||||
line-height: 1.6em;
|
||||
}
|
||||
h1 {
|
||||
font-size: 3em;
|
||||
font-weight: normal;
|
||||
margin: 0 0 1em;
|
||||
}
|
||||
h2 {
|
||||
font-size: 1.8em;
|
||||
font-weight: normal;
|
||||
margin: 3em 0 1em;
|
||||
}
|
||||
h3 {
|
||||
font-size: 1.2em;
|
||||
font-weight: bold;
|
||||
margin: 2em 0 1em;
|
||||
}
|
||||
a {
|
||||
color: #1E88E5;
|
||||
}
|
||||
</style>
|
||||
<link rel="stylesheet" href="text.css">
|
||||
</head>
|
||||
<body>
|
||||
<canvas id="map"></canvas>
|
||||
|
||||
<script src="pagemap.min.js"></script>
|
||||
<script>
|
||||
pagemap(document.querySelector('#map'));
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<header>
|
||||
<h1>At Eos Accusamu</h1>
|
||||
@@ -77,12 +43,5 @@
|
||||
<p> Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, <a href="?">eaque veritatis</a> et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.</p>
|
||||
</section>
|
||||
</main>
|
||||
|
||||
<canvas id="map"></canvas>
|
||||
|
||||
<script src="../dist/pagemap.js"></script>
|
||||
<script>
|
||||
pagemap(document.querySelector('#map'));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
176
src/pagemap.js
Normal file
176
src/pagemap.js
Normal file
@@ -0,0 +1,176 @@
|
||||
const win = global.window;
|
||||
const doc = win.document;
|
||||
const doc_el = doc.documentElement;
|
||||
const body = doc.querySelector('body');
|
||||
|
||||
const black = pc => `rgba(0,0,0,${pc / 100})`;
|
||||
const defaults = {
|
||||
viewport: null,
|
||||
styles: {
|
||||
'header,footer,section,article': black(8),
|
||||
'h1,a': black(10),
|
||||
'h2,h3,h4': black(8)
|
||||
},
|
||||
back: black(2),
|
||||
view: black(5),
|
||||
drag: black(10),
|
||||
interval: null
|
||||
};
|
||||
|
||||
const _listener = (el, method, types, fn) => types.split(/\s+/).forEach(type => el[method](type, fn));
|
||||
const on = (el, types, fn) => _listener(el, 'addEventListener', types, fn);
|
||||
const off = (el, types, fn) => _listener(el, 'removeEventListener', types, fn);
|
||||
|
||||
const Rect = (x, y, w, h) => { return {x, y, w, h}; };
|
||||
const rect_rel_to = (rect, pos = {x: 0, y: 0}) => Rect(rect.x - pos.x, rect.y - pos.y, rect.w, rect.h);
|
||||
|
||||
const rect_of_doc = () => {
|
||||
return Rect(0, 0, doc_el.scrollWidth, doc_el.scrollHeight);
|
||||
};
|
||||
|
||||
const rect_of_win = () => {
|
||||
return Rect(win.pageXOffset, win.pageYOffset, doc_el.clientWidth, doc_el.clientHeight);
|
||||
};
|
||||
|
||||
const el_get_offset = el => {
|
||||
const br = el.getBoundingClientRect();
|
||||
return {x: br.left + win.pageXOffset, y: br.top + win.pageYOffset};
|
||||
};
|
||||
|
||||
const rect_of_el = el => {
|
||||
const {x, y} = el_get_offset(el);
|
||||
return Rect(x, y, el.offsetWidth, el.offsetHeight);
|
||||
};
|
||||
|
||||
const rect_of_viewport = el => {
|
||||
const {x, y} = el_get_offset(el);
|
||||
return Rect(x + el.clientLeft, y + el.clientTop, el.clientWidth, el.clientHeight);
|
||||
};
|
||||
|
||||
const rect_of_content = el => {
|
||||
const {x, y} = el_get_offset(el);
|
||||
return Rect(x + el.clientLeft - el.scrollLeft, y + el.clientTop - el.scrollTop, el.scrollWidth, el.scrollHeight);
|
||||
};
|
||||
|
||||
module.exports = (canvas, options) => {
|
||||
const settings = Object.assign({}, defaults, options);
|
||||
const context = canvas.getContext('2d');
|
||||
|
||||
const calc_scale = (() => {
|
||||
const width = canvas.clientWidth;
|
||||
const height = canvas.clientHeight;
|
||||
return (w, h) => Math.min(width / w, height / h);
|
||||
})();
|
||||
|
||||
const resize_canvas = (w, h) => {
|
||||
canvas.width = w;
|
||||
canvas.height = h;
|
||||
canvas.style.width = `${w}px`;
|
||||
canvas.style.height = `${h}px`;
|
||||
};
|
||||
|
||||
const viewport = settings.viewport;
|
||||
const find = sel => Array.from((viewport || doc).querySelectorAll(sel));
|
||||
|
||||
let drag = false;
|
||||
|
||||
let root_rect;
|
||||
let view_rect;
|
||||
let scale;
|
||||
let drag_rx;
|
||||
let drag_ry;
|
||||
|
||||
const draw_rect = (rect, col) => {
|
||||
if (!col) {
|
||||
return;
|
||||
}
|
||||
context.beginPath();
|
||||
context.rect(rect.x, rect.y, rect.w, rect.h);
|
||||
context.fillStyle = col;
|
||||
context.fill();
|
||||
};
|
||||
|
||||
const apply_styles = styles => {
|
||||
Object.keys(styles).forEach(sel => {
|
||||
const col = styles[sel];
|
||||
find(sel).forEach(el => {
|
||||
draw_rect(rect_rel_to(rect_of_el(el), root_rect), col);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const draw = () => {
|
||||
root_rect = viewport ? rect_of_content(viewport) : rect_of_doc();
|
||||
view_rect = viewport ? rect_of_viewport(viewport) : rect_of_win();
|
||||
scale = calc_scale(root_rect.w, root_rect.h);
|
||||
|
||||
resize_canvas(root_rect.w * scale, root_rect.h * scale);
|
||||
|
||||
context.setTransform(1, 0, 0, 1, 0, 0);
|
||||
context.clearRect(0, 0, canvas.width, canvas.height);
|
||||
context.scale(scale, scale);
|
||||
|
||||
draw_rect(rect_rel_to(root_rect, root_rect), settings.back);
|
||||
apply_styles(settings.styles);
|
||||
draw_rect(rect_rel_to(view_rect, root_rect), drag ? settings.drag : settings.view);
|
||||
};
|
||||
|
||||
const on_drag = ev => {
|
||||
ev.preventDefault();
|
||||
const cr = rect_of_viewport(canvas);
|
||||
const x = (ev.pageX - cr.x) / scale - view_rect.w * drag_rx;
|
||||
const y = (ev.pageY - cr.y) / scale - view_rect.h * drag_ry;
|
||||
|
||||
if (viewport) {
|
||||
viewport.scrollLeft = x;
|
||||
viewport.scrollTop = y;
|
||||
} else {
|
||||
win.scrollTo(x, y);
|
||||
}
|
||||
draw();
|
||||
};
|
||||
|
||||
const on_drag_end = ev => {
|
||||
drag = false;
|
||||
canvas.style.cursor = 'pointer';
|
||||
body.style.cursor = 'auto';
|
||||
off(win, 'mousemove', on_drag);
|
||||
off(win, 'mouseup', on_drag_end);
|
||||
on_drag(ev);
|
||||
};
|
||||
|
||||
const on_drag_start = ev => {
|
||||
drag = true;
|
||||
|
||||
const cr = rect_of_viewport(canvas);
|
||||
const vr = rect_rel_to(view_rect, root_rect);
|
||||
drag_rx = ((ev.pageX - cr.x) / scale - vr.x) / vr.w;
|
||||
drag_ry = ((ev.pageY - cr.y) / scale - vr.y) / vr.h;
|
||||
if (drag_rx < 0 || drag_rx > 1 || drag_ry < 0 || drag_ry > 1) {
|
||||
drag_rx = 0.5;
|
||||
drag_ry = 0.5;
|
||||
}
|
||||
|
||||
canvas.style.cursor = 'crosshair';
|
||||
body.style.cursor = 'crosshair';
|
||||
on(win, 'mousemove', on_drag);
|
||||
on(win, 'mouseup', on_drag_end);
|
||||
on_drag(ev);
|
||||
};
|
||||
|
||||
const init = () => {
|
||||
canvas.style.cursor = 'pointer';
|
||||
on(canvas, 'mousedown', on_drag_start);
|
||||
on(viewport || win, 'load resize scroll', draw);
|
||||
if (settings.interval > 0) {
|
||||
setInterval(() => draw(), settings.interval);
|
||||
}
|
||||
draw();
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
return {
|
||||
redraw: draw
|
||||
};
|
||||
};
|
@@ -1,28 +0,0 @@
|
||||
const webpack = require('webpack');
|
||||
const pkg = require('./package.json');
|
||||
const banner = `${pkg.name} v${pkg.version} - ${pkg.homepage}`;
|
||||
|
||||
module.exports = {
|
||||
entry: './lib/pagemap.js',
|
||||
output: {
|
||||
path: './dist',
|
||||
filename: 'pagemap.js',
|
||||
library: 'pagemap',
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
module: {
|
||||
loaders: [{
|
||||
test: /\.js$/,
|
||||
exclude: /node_modules/,
|
||||
loader: 'babel-loader',
|
||||
query: {
|
||||
cacheDirectory: true,
|
||||
presets: ['es2015']
|
||||
}
|
||||
}]
|
||||
},
|
||||
plugins: [
|
||||
new webpack.BannerPlugin(banner),
|
||||
new webpack.optimize.UglifyJsPlugin()
|
||||
]
|
||||
};
|
Reference in New Issue
Block a user