Created
July 14, 2015 17:30
-
-
Save PifyZ/8de0dc84795a9575027e to your computer and use it in GitHub Desktop.
Haxe javascript-like canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package hx; | |
import openfl.display.Bitmap; | |
import openfl.display.BitmapData; | |
import openfl.display.PixelSnapping; | |
import openfl.display.Sprite; | |
import openfl.display.Graphics; | |
import openfl.display.DisplayObject; | |
import openfl.geom.Matrix; | |
import openfl.geom.Rectangle; | |
import openfl.filters.GlowFilter; | |
import openfl.filters.BitmapFilterQuality; | |
import openfl.text.TextField; | |
import openfl.text.TextFieldAutoSize; | |
import openfl.text.TextFormat; | |
typedef Transform = { | |
var scaleX:Float; | |
var scaleY:Float; | |
var translateX:Float; | |
var translateY:Float; | |
} | |
class HxCanvas { | |
public var sprite:Sprite; | |
public var graphics:Graphics; | |
public var saves:Array<Transform>; | |
public var scaleX:Float = 1; | |
public var scaleY:Float = 1; | |
public var angle:Float = 0; | |
public var translateX:Float = 0; | |
public var translateY:Float = 0; | |
public var width:UInt = 300; | |
public var height:UInt = 150; | |
public var globalAlpha:Float = 1.0; | |
public var globalCompositeOperation:String = "source-over"; | |
public var lineWidth:Float = 1.0; | |
public var lineCap:String = "butt"; | |
public var lineJoin:String = "miter"; | |
public var meterLimit:Float = 10; | |
public var strokeStyle:Dynamic = "black"; | |
public var fillStyle:Dynamic = "black"; | |
public var shadowOffsetX:Float = 0.0; | |
public var shadowOffsetY:Float = 0.0; | |
public var shadowBlur:Float = 0.0; | |
public var shadowColor:String = "transparent black"; | |
public var font:String = "10px sans-serif"; | |
public var textAlign:String = "start"; | |
public var textBaseline:String = "alphabetic"; | |
public function new() { | |
sprite = new Sprite(); | |
graphics = sprite.graphics; | |
saves = new Array<Transform>(); | |
} | |
public function toDataURL(type:String, args:Array<Dynamic>):String { | |
return ""; | |
} | |
public function save():Void { | |
saves.push({ | |
scaleX: scaleX, | |
scaleY: scaleY, | |
translateX: translateX, | |
translateY: translateY | |
}); | |
} | |
public function restore():Void { | |
var save:Transform = saves.pop(); | |
if (save != null) { | |
scaleX = save.scaleX; | |
scaleY = save.scaleY; | |
translateX = save.translateX; | |
translateY = save.translateY; | |
} | |
} | |
public function scale(x:Float, y:Float):Void { | |
scaleX = x; | |
scaleY = y; | |
} | |
public function rotate(angle:Float):Void { | |
this.angle = angle; | |
} | |
public function translate(x:Float, y:Float):Void { | |
translateX = x; | |
translateY = y; | |
} | |
public function transform(m11:Float, m12:Float, m21:Float, m22:Float, dx:Float, dy:Float):Void { | |
} | |
public function setTransform(m11:Float, m12:Float, m21:Float, m22:Float, dx:Float, dy:Float):Void { | |
} | |
/* | |
public function drawImage(image:Bitmap, dx:Float, dy:Float, ?dw:Float, ?dh:Float):Void { | |
var s = new Sprite(); | |
s.addChild(image); | |
s.x = dx; | |
s.y = dy; | |
s.width = dw; | |
s.height = dh; | |
sprite.addChild(s); | |
} | |
public function drawCroppedImage(image:Bitmap, sx:Float, sy:Float, sw:Int, sh:Int, dx:Float, dy:Float):Void { | |
var cropArea:Rectangle = new Rectangle(0, 0, sw, sh); | |
var croppedBitmap:Bitmap = new Bitmap(new BitmapData(sw, sh), PixelSnapping.ALWAYS, true); | |
croppedBitmap.bitmapData.draw( | |
image, | |
new Matrix(1, 0, 0, 1, -sx, -sy), | |
null, | |
null, | |
cropArea, | |
true | |
); | |
var s = new Sprite(); | |
s.addChild(croppedBitmap); | |
s.x = dx; | |
s.y = dy; | |
sprite.addChild(s); | |
} | |
*/ | |
public function drawImage( | |
image:Bitmap, | |
sx:Float, | |
sy:Float, | |
sw:Null<Float> = null, | |
sh:Null<Float> = null, | |
dx:Null<Float> = null, | |
dy:Null<Float> = null, | |
dw:Null<Float> = null, | |
dh:Null<Float> = null | |
):Void { | |
if (sw == null) sw = image.bitmapData.width; | |
if (sh == null) sh = image.bitmapData.height; | |
if (dx == null) dx = 0; | |
if (dy == null) dy = 0; | |
if (dw == null) dw = sw; | |
if (dh == null) dh = sh; | |
trace(sw); | |
var cropArea:Rectangle = new Rectangle(0, 0, sw, sh); | |
var ssw = Std.int(sw); | |
var ssh = Std.int(sh); | |
var croppedBitmap:Bitmap = new Bitmap( | |
new BitmapData(ssw, ssh, true, 0x00000000), | |
PixelSnapping.ALWAYS, | |
true); | |
croppedBitmap.bitmapData.draw( | |
image, | |
new Matrix(1, 0, 0, 1, -sx, -sy), | |
null, | |
null, | |
cropArea, | |
true); | |
var s = new Sprite(); | |
s.addChild(croppedBitmap); | |
s.x = dx; | |
s.y = dy; | |
s.width = dw; | |
s.height = dh; | |
sprite.addChild(s); | |
} | |
public function createLinearGradient(x0:Float, y0:Float, x1:Float, y1:Float):CanvasGradient { | |
return null; | |
} | |
public function createRadialGradient(x0:Float, y0:Float, r0:Float, x1:Float, y1:Float, r1:Float):CanvasGradient { | |
return null; | |
} | |
public function createPattern(image:Dynamic, repetition:String):CanvasPattern { | |
return null; | |
} | |
public function addColorStop(offset:Float, color:String):Void { | |
} | |
public function beginPath():Void { | |
graphics.beginFill(fillStyle); | |
} | |
public function closePath():Void { | |
graphics.endFill(); | |
} | |
public function fill():Void { | |
graphics.endFill(); | |
} | |
public function stroke():Void { | |
graphics.endFill(); | |
} | |
public function clip():Void { | |
} | |
public function moveTo(x:Float, y:Float):Void { | |
graphics.moveTo(x, y); | |
} | |
public function lineTo(x:Float, y:Float):Void { | |
graphics.lineTo(x, y); | |
} | |
public function quatraticCurveTo(cpx:Float, cpy:Float, x:Float, y:Float):Void { | |
graphics.curveTo(cpx, cpy, x, y); | |
} | |
public function bezierCurveTo(cp1x:Float, cp1y:Float, cp2x:Float, cp2y:Float, x:Float, y:Float):Void { | |
} | |
public function arcTo(x1:Float, y1:Float, x2:Float, y2:Float, radius:Float):Void { | |
} | |
public function arc(x:Float, y:Float, startAngle:Float, endAngle:Float, anticlockwise:Bool):Void { | |
} | |
public function rect(x:Float, y:Float, w:Float, h:Float):Void { | |
graphics.endFill(); | |
} | |
public function isPointInPath(x:Float, y:Float):Bool { | |
return false; | |
} | |
public function fillText(text:String, x:Float, y:Float, ?maxWidth:Float):Void { | |
// à faire : maxWidth n'est pas utilisé | |
var txtFormat = new TextFormat(); | |
txtFormat.color = fillStyle; | |
var txt = new TextField(); | |
txt.text = text; | |
txt.x = x; | |
txt.y = y; | |
txt.autoSize = TextFieldAutoSize.LEFT; | |
txt.width = txt.textWidth; | |
txt.height = txt.textHeight; | |
txt.selectable = false; | |
txt.alpha = globalAlpha; | |
txt.setTextFormat(txtFormat); | |
formatObject(txt); | |
sprite.addChild(txt); | |
} | |
public function strokeText(text:String, x:Float, y:Float, ?maxWidth:Float):Void { | |
var outline = new GlowFilter(strokeStyle, globalAlpha, lineWidth, lineWidth, 10); | |
outline.quality = BitmapFilterQuality.MEDIUM; | |
var txtFormat = new TextFormat(); | |
txtFormat.color = 0x00000000; | |
// à faire : maxWidth n'est pas utilisé | |
var txt = new TextField(); | |
txt.text = text; | |
txt.x = x; | |
txt.y = y; | |
txt.autoSize = TextFieldAutoSize.LEFT; | |
txt.width = txt.textWidth; | |
txt.height = txt.textHeight; | |
txt.selectable = false; | |
txt.filters = [ outline ]; | |
formatObject(txt); | |
sprite.addChild(txt); | |
} | |
public function measureText(text:String):Float { | |
var txt = new TextField(); | |
txt.text = text; | |
txt.autoSize = TextFieldAutoSize.LEFT; | |
txt.width = txt.textWidth; | |
txt.height = txt.textHeight; | |
return txt.width; | |
} | |
public function clearRect(x:Float, y:Float, w:Float, h:Float):Void { | |
sprite.graphics.clear(); // à faire : x, y, w et h ne sont pas pris en compte | |
while (sprite.numChildren > 0) | |
sprite.removeChildAt(0); | |
} | |
public function fillRect(x:Float, y:Float, w:Float, h:Float):Void { | |
var rect = new Sprite(); | |
rect.graphics.beginFill(fillStyle); | |
rect.graphics.drawRect(x, y, w, h); | |
formatObject(rect); | |
sprite.addChild(rect); | |
} | |
public function strokeRect(x:Float, y:Float, w:Float, h:Float):Void { | |
var rect = new Sprite(); | |
rect.graphics.lineStyle(lineWidth, strokeStyle); | |
rect.graphics.beginFill(0x000000, 0); | |
rect.graphics.drawRect(x, y, w, h); | |
formatObject(rect); | |
sprite.addChild(rect); | |
} | |
public function createImageData(sw:Float, sh:Float):ImageData { | |
return null; | |
} | |
public function createImageDataFromImageData(imageData:ImageData):ImageData { | |
return null; | |
} | |
public function getImageData(sx:Float, sy:Float, sw:Float, sh:Float):ImageData { | |
return null; | |
} | |
public function putImageData(imageData:ImageData, dx:Float, dy:Float, ?dirtyX:Float, ?dirtyY:Float, ?dirtyWidth:Float, ?dirtyHeight:Float):Void { | |
} | |
private function formatObject(obj:DisplayObject) { | |
if (scaleX != 1) | |
obj.scaleX = scaleX; | |
if (scaleY != 1) | |
obj.scaleY = scaleY; | |
if (angle != 0) { | |
var matrix:Matrix = obj.transform.matrix; | |
var bounds:Rectangle = obj.getBounds(obj.parent); | |
matrix.translate(-(bounds.left + (bounds.width / 2)), -(bounds.top + (bounds.height / 2))); | |
matrix.rotate((angle / 180) * Math.PI); | |
matrix.translate(bounds.left + (bounds.width / 2), bounds.top + (bounds.height / 2)); | |
obj.transform.matrix = matrix; | |
obj.rotation = Math.round(obj.rotation); | |
} | |
} | |
} | |
class TextMetrics { | |
public var width:Float; | |
} | |
class ImageData { | |
public var width:UInt; | |
public var height:UInt; | |
public var data:Dynamic; | |
public var length:UInt; | |
} | |
class CanvasGradient { } | |
class CanvasPattern { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment