📄 rectmarquee.js
字号:
var Marquee = Class.create();
Marquee.prototype = {
BASE: 1,
MARQUEE: 2,
OPACITY: 4,
EDGE: 8,
items: [],
options: {},
maxZIndex: 1,
ratio: 0,
landingobj: null,
initialize: function(element) {
this.onFinishDrag = this.finishDrag.bindAsEventListener(this);
this.onUpdateDrag = this.updateDrag.bindAsEventListener(this);
this.onWindResize = this.render.bindAsEventListener(this);
this.onKeyPressed = this.keyPress.bindAsEventListener(this);
Event.observe(document, "mouseup", this.onFinishDrag);
Event.observe(document, "mousemove", this.onUpdateDrag);
Event.observe(window, 'resize', this.onWindResize);
if (element) {
this.add(element, arguments[1]);
};
},
add: function(element) {
element = $(element);
if (!element) return;
var options = Object.extend({
ratio: 0,
preview: false,
previewWidth: 120,
previewHeight: 120,
onUpdate: null,
onBeforeUpdate: null,
color: 'black',
opacity: 0.75,
type: 'crop', // window or crop, marquee or rect, other
allowResize: true,
allowHotKeys: true,
hideEdges: false,
coords: {x1: 0, y1: 0, x2: 0, y2: 0, width: 0, height: 0}
}, arguments[1] || {});
var id = this.items.length;
this.activeId = id;
var landing = document.createElement('DIV');
landing.className = "marquee-landing";
landing.mtype = this.BASE;
landing.id = 'rmt_landing_' + id;
landing.onselectstart = this._bibb;
var pos = Position.positionedOffset(element);
Element.setStyle(landing, {
width: element.offsetWidth + 'px',
height: element.offsetHeight + 'px',
zIndex: this.getMaxZIndex(),
left: pos[0] + 'px',
top: pos[1] + 'px'
});
var marquee = document.createElement('DIV');
marquee.className = "marquee-element";
marquee.mtype = this.MARQUEE;
marquee.id = 'rmt_marquee_' + id;
marquee.onselectstart = this._bibb;
Element.hide(marquee);
var wind = document.createElement('DIV');
wind.className = "marquee-window";
wind.id = 'rmt_window_' + id;
wind.mtype = this.MARQUEE;
wind.number = 5;
wind.onselectstart = this._bibb;
marquee.appendChild(wind);
var o1 = document.createElement('DIV');
o1.className = "marquee-opacity";
o1.mtype = this.OPACITY;
o1.onselectstart = this._bibb;
var o2 = o1.cloneNode(true);
o2.mtype = this.OPACITY;
o2.onselectstart = this._bibb;
Element.setStyle(o2, {left: 0});
var o3 = o1.cloneNode(true);
o3.mtype = this.OPACITY;
o3.onselectstart = this._bibb;
Element.setStyle(o3, {right: 0});
var o4 = o1.cloneNode(true);
o4.mtype = this.OPACITY;
o4.onselectstart = this._bibb;
Element.setStyle(o1, {
top: 0,
left: 0,
width: "100%"
});
Element.setStyle(o4, {
bottom: 0,
left: 0,
width: "100%"
});
marquee.o1 = o1;
marquee.o2 = o2;
marquee.o3 = o3;
marquee.o4 = o4;
var listener = document.createElement('INPUT');
listener.className = "marquee-listener";
listener.style.zIndex = this.getMaxZIndex();
listener.onselectstart = this._bibb;
listener.id = 'rmt_listener_' + id;
Event.observe(listener, "keydown", this.onKeyPressed);
var edge = document.createElement('DIV');
edge.className = "marquee-edge";
edge.onselectstart = this._bibb;
var edge1 = edge.cloneNode(true);
edge1.number = 1;
edge1.mtype = this.EDGE;
marquee.appendChild(edge1);
Element.setStyle(edge1, {left: '0%', top: '0%', cursor: 'nw-resize'});
var edge2 = edge.cloneNode(true);
edge2.number = 2;
edge2.mtype = this.EDGE;
marquee.appendChild(edge2);
Element.setStyle(edge2, {left: '50%', top: '0%', cursor: 'n-resize'});
var edge3 = edge.cloneNode(true);
edge3.number = 3;
edge3.mtype = this.EDGE;
marquee.appendChild(edge3);
Element.setStyle(edge3, {left: '100%', top: '0%', cursor: 'ne-resize'});
var edge4 = edge.cloneNode(true);
edge4.number = 4;
edge4.mtype = this.EDGE;
marquee.appendChild(edge4);
Element.setStyle(edge4, {left: '0%', top: '50%', cursor: 'w-resize'});
var edge6 = edge.cloneNode(true);
edge6.number = 6;
edge6.mtype = this.EDGE;
marquee.appendChild(edge6);
Element.setStyle(edge6, {left: '100%', top: '50%', cursor: 'w-resize'});
var edge7 = edge.cloneNode(true);
edge7.mtype = this.EDGE;
edge7.number = 7;
marquee.appendChild(edge7);
Element.setStyle(edge7, {left: '0%', top: '100%', cursor: 'ne-resize'});
var edge8 = edge.cloneNode(true);
edge8.number = 8;
edge8.mtype = this.EDGE;
Element.setStyle(edge8, {left: '50%', top: '100%', cursor: 'n-resize'});
marquee.appendChild(edge8);
var edge9 = edge.cloneNode(true);
edge9.number = 9;
edge9.mtype = this.EDGE;
Element.setStyle(edge9, {left: '100%', top: '100%', cursor: 'nw-resize'});
marquee.appendChild(edge9);
marquee.number = 5;
landing.appendChild(o1);
landing.appendChild(o2);
landing.appendChild(o3);
landing.appendChild(o4);
landing.appendChild(marquee);
landing.appendChild(listener);
element.parentNode.insertBefore(landing, element);
var preview = $(options.preview);
if (preview) {
var pImage = new Image();
pImage.src = element.tagName == 'IMG' ? element.src : element.style.backgroundImage;
pImage.className = "marquee-preview";
preview.appendChild(pImage);
Element.setStyle(preview, {
overflow: 'hidden',
position: preview.style.position == 'absolute' ? 'absolute' : 'relative',
width: 0, height: 0,
fontSize: '1px', lineHeight: '0%'
});
};
options.onLandClick = onLandClick;
this.items.push({
id: id,
marquee: marquee,
element: element,
coords: options.coords,
wind: wind,
options: options,
preview: preview,
pImage: pImage,
listener: listener,
zIndex: this.getMaxZIndex()
});
this.setRatio(options.ratio);
this.maxZIndex++;
this.setOpacity(options.opacity);
this.setColor(options.color);
this.setType(options.type);
if (options.hideEdges) this.hideEdges();
if (options.coords.width || options.coords.x2)
this.setCoords(
options.coords.x1,
options.coords.y1,
options.coords.width ? options.coords.width : options.coords.x2 - options.coords.x1,
options.coords.height ? options.coords.height : options.coords.y2 - options.coords.y1
);
var onLandClick = this.initDrag.bind(this, id);
var onBeforeFocus = this.setFocus.bind(this, id);
Event.observe(landing, 'mousedown', onLandClick);
Event.observe(landing, 'mouseup', onBeforeFocus);
Event.observe(landing, 'dblclick', sendCropAndResize);
this.landingobj = landing;
},
setFocus: function(id) {
if (!this.getOption(id, 'allowHotKeys')) return;
var listener = this.items[id].listener;
listener.focus();
},
setRandomColor: function() {
var numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var r1 = Math.round(Math.random()*15);
var r2 = Math.round(Math.random()*15);
var r3 = Math.round(Math.random()*15);
this.setColor('#' + numbers[r1] + numbers[r2] + numbers[r3]);
},
enable: function(id) {
Event.observe(this.getMarquee(id).parentNode, "mousedown", this.getOption(id, 'onLandClick'));
},
disable: function(id) {
Event.stopObserving(this.getMarquee(id).parentNode, "mousedown", this.getOption(id, 'onLandClick'));
},
getId: function() {
return this.activeId;
},
accessResize: function(access, id) {
access = (access == undefined) ? true : false;
this.setOption(id, 'allowResize', access);
if (access) this.showEdges(id); else this.hideEdges(id);
},
accessHotKeys: function(access, id) {
access = (access == undefined) ? true : false;
this.setOption(id, 'allowHotKeys', access);
},
getMaxZIndex: function() {
return this.maxZIndex;
},
setRatio: function(ratio) {
this.items[this.activeId].options.ratio = ratio;
var marquee = this.items[this.activeId].marquee;
if (!this.getOption(this.activeId, 'allowResize') || this.getOption(this.activeId, 'hideEdges')) return;
$A(marquee.childNodes).each(function(item){
if (item.className && item.className == 'marquee-edge') {
item.style.display = (item.number % 2 == 0) && ratio ? 'none' : 'block';
};
});
},
getRatio: function(id) {
return this.ratio ? this.ratio : this.items[id || this.activeId].options.ratio;
},
initDrag: function(id, event) {
event = event || window.event;
if (!event) return;
var element = Event.element(event);
var marquee = this.getMarquee(id);
if (!marquee || !element.mtype) return;
var image = this.getElement(id);
this.dragging = true;
Position.prepare();
Element.show(marquee);
this.startPosition = Position.cumulativeOffset(image);
this.startOffset = Position.positionedOffset(marquee);
this.startDimension = Element.getDimensions(marquee);
this.startMovingPoint = [Event.pointerX(event) - this.startPosition[0], Event.pointerY(event) - this.startPosition[1]];
this.activeEdge = element.number;
this.activeId = id;
if (element.mtype & (this.OPACITY | this.BASE)) {
this.startOffset = this.startMovingPoint;
};
Event.stop(event);
},
_bibb: function(event) {
return false;
},
updateDrag: function(event) {
if (!this.dragging) return;
// fix for IE - onmousemove very frequent listening
this._currentTime = new Date();
if ((this._currentTime - this._lastUpdate) < 32) {
return;
};
this._lastUpdate = new Date();
var options = this.items[this.activeId].options;
if (typeof options.onBeforeUpdate == 'function' && !options.onBeforeUpdate()) return;
var ratio = this.getRatio();
if (event.shiftKey && !ratio) {
this.ratio = 1;
};
var pointer = [Event.pointerX(event) - this.startPosition[0], Event.pointerY(event) - this.startPosition[1]];
var shifting = [pointer[0] - this.startMovingPoint[0], pointer[1] - this.startMovingPoint[1]];
switch (this.activeEdge) {
case 1:
shifting = this.snap(shifting[0], shifting[1], 1);
var newDimension = {
width: this.startDimension.width - shifting[0],
height: this.startDimension.height - shifting[1]
};
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 2:
var newDimension = {
width: this.startDimension.width,
height: this.startDimension.height - shifting[1]
};
shifting[0] = 0;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 3:
shifting = this.snap(shifting[0], shifting[1], -1);
var newDimension = {
width: this.startDimension.width + shifting[0],
height: this.startDimension.height - shifting[1]
};
shifting[0] = 0;
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 4:
var newDimension = {
width: this.startDimension.width - shifting[0],
height: this.startDimension.height
};
shifting[1] = 0;
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
break;
case 6:
var newDimension = {
width: this.startDimension.width + shifting[0],
height: this.startDimension.height
};
shifting[0] = 0;
shifting[1] = 0;
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
break;
case 7:
shifting = this.snap(shifting[0], shifting[1], -1);
var newDimension = {
width: this.startDimension.width - shifting[0],
height: this.startDimension.height + shifting[1]
};
shifting[1] = 0;
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 8:
var newDimension = {
width: this.startDimension.width,
height: this.startDimension.height + shifting[1]
};
shifting[0] = 0;
shifting[1] = 0;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 9:
shifting = this.snap(shifting[0], shifting[1], 1);
var newDimension = {
width: this.startDimension.width + shifting[0],
height: this.startDimension.height + shifting[1]
};
shifting[0] = 0;
shifting[1] = 0;
if (newDimension.width - 2 < 0)
shifting[0] += newDimension.width - 2;
if (newDimension.height - 2 < 0)
shifting[1] += newDimension.height - 2;
break;
case 5:
var newDimension = {
width: this.startDimension.width,
height: this.startDimension.height
};
break;
default:
shifting = this.snap(shifting[0], shifting[1], (shifting[1] && shifting[0]/shifting[1] < 0 ? -1 : 1));
if (!this.getOption(this.activeId, 'allowResize')) return;
var newDimension = {
width: shifting[0],
height: shifting[1]
};
if (newDimension.width - 2 >= 0)
shifting[0] = 0;
if (newDimension.height - 2 >= 0)
shifting[1] = 0;
newDimension.width += 2;
newDimension.height += 2;
};
newDimension.width -= 2;
newDimension.height -= 2;
if (this.activeEdge != 5) {
var element = this.getElement(this.activeId);
if (this.startOffset[0] + shifting[0] +
newDimension.width > element.offsetWidth) {
newDimension.width = element.offsetWidth -
this.startOffset[0] - shifting[0];
if (ratio) {
newDimension.height = newDimension.width / ratio;
};
} else if (this.startOffset[0] + shifting[0] -
newDimension.width > element.offsetWidth) {
newDimension.width = element.offsetWidth -
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -