位图9宫格 BitmapSlice9.jsfl
/**
* Version 1.1, May 4: fixed issue with symbols in library folders.
**/
/**
* BitmapSlice9 JSFL by Grant Skinner. Apr 13, 2010
* Visit www.gskinner.com/blog for documentation, updates and more free code.
*
*
* Copyright (c) 2010 Grant Skinner
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
**/ var doc = fl.getDocumentDOM();
var minAsVersion = 2;
var minPlayerVersion = 8;
var selection = null;
var layer = null;
var grid = null; run(); function run() {
if (!checkDocument()) { return; }
if (!checkSelection()) { return; }
if (!checkScale9()) { return; } // everything checks out.
slice();
} function checkDocument() {
if (doc == null) {
alert('You must have an FLA open as your active document to run this command.');
return false;
}
if (doc.asVersion < minAsVersion || parseInt(doc.getPlayerVersion()) < minPlayerVersion) {
alert('The Scale9 feature must target Flash Player ' + minPlayerVersion + ' and ActionScript ' + minAsVersion + '.');
return false;
}
return true;
} function checkSelection() {
var selectedItems = doc.selection;
if (selectedItems.length != 1 || selectedItems[0].instanceType != "bitmap") {
alert('Please select a single bitmap object on the stage before running this command.');
return false;
} var selectedFrames = doc.getTimeline().getSelectedFrames();
if (selectedFrames.length != 3) {
alert('Only a single keyframe should be selected before running this command.');
return false;
}
selection = selectedItems[0];
layer = doc.getTimeline().layers[selectedFrames[0]];
frame = layer.frames[selectedFrames[1]];
return true;
} function checkScale9() {
var item = doc.getTimeline().libraryItem; //CS5
if (item == null) {
doc.library.selectItem(doc.getTimeline().name,true,true);
item = doc.library.getSelectedItems()[0];
}
if (item == null) {
alert('The selected bitmap must be in a MovieClip symbol.');
return false;
}
if (!item.scalingGrid) {
if (confirm('Scale 9 must be enabled for the parent symbol. Would you like to enable this now?')) {
item.scalingGrid = true;
}
return false;
}
grid = item.scalingGridRect;
grid = {x:grid.left, y:grid.top, width:grid.right-grid.left, height:grid.bottom-grid.top};
return true;
} function slice() {
var timeline = doc.getTimeline();
var bmp = selection.libraryItem;
var name = bmp.name;
if (name.indexOf(".") > 0) { name = name.substr(0,name.indexOf(".")); }
if (name.indexOf("/") > 0) { name = name.substr(name.lastIndexOf("/")+1); }
var sliceLayer = null;
var bmpLayer = null;
var index; // check if our selection is already on an existing _bmp layer:
if (layer.name.substr(-4) == "_bmp") {
bmpLayer = layer;
name = layer.name.substr(0,layer.name.length-4);
} // check if the slice layer already exists:
var sliceLayerIndexes = timeline.findLayerIndex(name+"_slices");
if (sliceLayerIndexes != null && sliceLayerIndexes.length > 0) {
sliceLayer = timeline.layers[sliceLayerIndexes[0]];
} // create or rename bmpLayer if needed:
if (bmpLayer == null) {
// create a bmpLayer if there are other elements on the current layer.
if (frame.elements.length > 1) {
// create new layer:
doc.clipCut();
if (sliceLayer) { timeline.setSelectedLayers(sliceLayerIndexes[0]); }
index = timeline.addNewLayer(name+"_bmp","guide",true);
bmpLayer = timeline.layers[index];
doc.clipPaste(true);
} else {
// rename the current layer:
layer.name = name+"_bmp";
bmpLayer = layer;
}
} // set up bmpLayer properties:
bmpLayer.visible = false; // hidden
bmpLayer.layerType = "guide"; // avoid compiling it into the SWF if (sliceLayer) {
// sliceLayer already exists, clear old slices:
if (selectSlices(sliceLayer)) { document.deleteSelection(); }
} else {
// create new sliceLayer below the bmpLayer:
index = timeline.addNewLayer(name+"_slices","normal",false);
sliceLayer = timeline.layers[index];
} // ensure the sliceLayer is selected:
timeline.setSelectedLayers(timeline.findLayerIndex(sliceLayer.name)[0]); // find the original size and path of the library bitmap
var bmpWidth = selection.hPixels;
var bmpHeight = selection.vPixels;
var bmpPath = bmp.name; // do the slicing:
var srcRect = {x:selection.x, y:selection.y, width:selection.width, height:selection.height}; var cols = [grid.x-10000, grid.x, grid.x+grid.width, 10000, grid.width, 10000];
var rows = [grid.y-10000, grid.y, grid.y+grid.height, 10000, grid.height, 10000]; for (var row=0; row<3; row++) {
for (var col=0; col<3; col++) {
var targetRect = getIntersection(srcRect, {x:cols[col], y:rows[row], width:cols[col+3], height:rows[row+3]});
drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect);
}
} selectSlices(sliceLayer);
} function selectSlices(sliceLayer) {
var elements = sliceLayer.frames[0].elements;
var s = [];
for (var i=0; i<elements.length; i++) {
// only remove rectangle primitives, in case there are other items on the layer:
if (elements[i].elementType == "shape" && elements[i].isRectangleObject) {
s.push(elements[i]);
}
}
if (s.length > 0) {
document.selection = s;
}
return s.length > 0;
} function drawRect(bmpPath, bmpWidth, bmpHeight, srcRect, targetRect) {
if (targetRect == null) { return; }
var fill = doc.getCustomFill();
fill.style = "bitmap";
fill.bitmapIsClipped = false;
fill.bitmapPath = bmpPath;
var matrix = selection.matrix;
matrix.tx = srcRect.x;
matrix.ty = srcRect.y;
matrix.a = srcRect.width/(bmpWidth/20); // 20 seems to be a magic number for calculating the matrix.
matrix.d = srcRect.height/(bmpHeight/20);
matrix.b = matrix.c = 0;
fill.matrix = matrix;
doc.setCustomFill(fill);
doc.addNewPrimitiveRectangle({left:targetRect.x, top:targetRect.y, right:targetRect.x+targetRect.width, bottom:targetRect.y+targetRect.height},0,false,true);
} function getIntersection(rect1, rect2) {
var x = max(rect1.x, rect2.x);
var y = max(rect1.y, rect2.y);
var r = min(rect1.x+rect1.width, rect2.x+rect2.width);
var b = min(rect1.y+rect1.height, rect2.y+rect2.height);
if (r > x && b > y) {
return {x:x, y:y, width:r-x, height:b-y}
}
return null;
} function max(num1, num2) {
return (num1 > num2) ? num1 : num2;
}
function min(num1, num2) {
return (num1 < num2) ? num1 : num2;
}
位图9宫格 BitmapSlice9.jsfl的更多相关文章
- 宫格布局实例(注意jquery的版本号要统一)
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- 宫格布局实例(注意jquery的版本号要统一)2
<!DOCTYPE html><html><head><meta charset="utf-8" /><style> * ...
- css-九宫格自适应的实现
高度自适应使用padding 或 padding-bottom + 百分比来实现: 宽度自适应使用width + 百分比来实现. 下面是实现九宫格自适应的代码: <!DOCTYPE html&g ...
- HTML5 Canvas中9宫格的坑
近期小鸟情人游戏上了手机qq空间,一个3岁的游戏来了她的第二春.为了能有更好的表现,我们对其进行了一次改版. 改版当中一项就是对原来的弹出框样式进行改进.将大块木板材质改成纯色(边框为圆角金属材质)样 ...
- 微信小程序多宫格抽奖
最近闲来无事,做了一个多宫格抽奖的例子,有什么需要改进或者错误的地方,请留言,谢谢 首先看效果 思路是先让其转动2圈多,然后再进行抽奖,格子运动用的是setTimeout,让其运行的时间间隔不一样,然 ...
- Android自定义多宫格解锁控件
在此之前,一直在想九宫格的实现方法,经过一个上午的初步研究终于完成了一个简单的N*N的宫格解锁组件,代码略显粗糙,仅仅做到简单的实现,界面等后期在做优化,纯粹是学习的目的,在算法上有点缺陷,如果有错误 ...
- vue 如何拿到后台传回的富文本中的img,进行9宫格排列展示以及相关处理
描述: res.data.list 返回的数组, 数组中的每个对象有一个 content,就是传回来的富文本的内容,要拿到这里面的所有的img,进行9宫格排列处理: 1.let img = this. ...
- golang 六宫格、九宫格头像生成
图片示例就不传了,在原WordPress上. //Merge6Grid 6宫格 //rule NO1:至少3张图 最多6张图 // NO2:第一张大小 60*60 其他大小 28*28 间隔4px 合 ...
- Python爬虫学习笔记之微信宫格验证码的识别(存在问题)
本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...
随机推荐
- WinForm 换行问题 textbox (转)
WinForm 换行问题 textbox 今天碰到一段string在label中能正常换行,但是在textbox中却无法换行的问题. 首先考虑是换行符的问题.在网上查了些资料: 1.TextBox 中 ...
- kindeditor-4.1.10 结合 Asp.Net MVC 添加图片功能
KindEditor是一套开源的HTML可视化编辑器,现在我要结合Asp.Net MVC4 上传图片功能,做相应的配置和修改, 其实网上也有人写过类似的文章了,我写出来是以防以后使用的时候出现这样的问 ...
- [搜片神器]BT管理程序数据库速度调试优化问题
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 谢谢园子 ...
- vbe6ext.olb不能被加载 宏内存溢出
今天想玩一下PowerPoint的宏,却发现玩不起来!!! 另外,每次打开ppt时都会提示vbe6ext.olb不能加载. 网上说重新下载个vbe6ext.olb然后复制到相应的路径.我也试着下载,然 ...
- 无锁算法CAS 概述
无锁算法CAS 概述 JDK5.0以后的版本都引入了高级并发特性,大多数的特性在java.util.concurrent包中,是专门用于多线并发编程的,充分利用了现代多处理器和多核心系统的功能以编写大 ...
- js key事件 keyCode大全
keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...
- leetcode3 Two Sum III – Data structure design
Question: Design and implement a TwoSum class. It should support the following operations: add and f ...
- spoj 297
就是对距离进行二分找最大值 .... #include <cstring> #include <cstdio> #include <algorithm> #incl ...
- Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details. ShareSDK 也有这种错误
Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for ...
- PHP 7 值得期待的新特性(上)
这是我们期待已久的 PHP 7 系列文章的第一篇. 或许你已经知道了,我在 PHP 5.0.0 时间轴 提的 RFC (Request For Comments)通过了, PHP 7 成为 PHP 下 ...