Skip to content

Commit 3d5492b

Browse files
use hardware accelerated transforms (#28)
1 parent 70452b2 commit 3d5492b

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

src/Internal/Transform.elm

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ combine transform combined =
108108

109109
render : Combined -> String
110110
render combined =
111-
[ render_ translate_ combined.xy
112-
, render_ translateX_ combined.x
113-
, render_ translateY_ combined.y
111+
[ translate_ combined
114112
, render_ scale_ combined.scale
115113
, render_ rotate_ combined.rotate
116114
]
@@ -125,42 +123,64 @@ render_ f =
125123

126124
scale_ : ( Float, Float ) -> String
127125
scale_ ( x_, y_ ) =
128-
join [ "scale(", String.fromFloat x_, ",", String.fromFloat y_, ")" ]
126+
String.concat [ "scale3d(", String.fromFloat x_, ",", String.fromFloat y_, ",1)" ]
127+
128+
129+
translate_ : Combined -> String
130+
translate_ combined =
131+
case ( combined.xy, combined.x, combined.y ) of
132+
( Just ( x_, y_ ), _, _ ) ->
133+
translateXY_ x_ y_
134+
135+
( Nothing, Just x_, Nothing ) ->
136+
translateX_ x_
137+
138+
( Nothing, Nothing, Just y_ ) ->
139+
translateY_ y_
140+
141+
( Nothing, Just x_, Just y_ ) ->
142+
translateXY_ x_ y_
143+
144+
( Nothing, Nothing, Nothing ) ->
145+
""
146+
147+
148+
translateXY_ : Float -> Float -> String
149+
translateXY_ x_ y_ =
150+
String.concat [ "translate3d(", px x_, ",", px y_, ",0)" ]
129151

130152

131153
translateX_ : Float -> String
132154
translateX_ n =
133-
join [ "translateX(", px n, ")" ]
155+
String.concat [ "translate3d(", px n, ",0,0)" ]
134156

135157

136158
translateY_ : Float -> String
137159
translateY_ n =
138-
join [ "translateY(", px n, ")" ]
139-
140-
141-
translate_ : ( Float, Float ) -> String
142-
translate_ ( x_, y_ ) =
143-
join [ "translate(", px x_, ",", px y_, ")" ]
160+
String.concat [ "translate3d(0,", px n, ",0)" ]
144161

145162

146163
rotate_ : Float -> String
147164
rotate_ n =
148-
join [ "rotate(", deg n, ")" ]
149-
150-
151-
join : List String -> String
152-
join =
153-
String.join ""
165+
String.concat [ "rotate3d(0,0,1,", deg n, ")" ]
154166

155167

156168
px : Float -> String
157169
px n =
158-
String.fromFloat n ++ "px"
170+
if n == 0 then
171+
"0"
172+
173+
else
174+
String.fromFloat n ++ "px"
159175

160176

161177
deg : Float -> String
162178
deg n =
163-
String.fromFloat n ++ "deg"
179+
if n == 0 then
180+
"0"
181+
182+
else
183+
String.fromFloat n ++ "deg"
164184

165185

166186

tests/FromToTest.elm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ suite =
3232
[ P.opacity 1, P.x 50 ]
3333
[ P.opacity 0.5, P.x 100 ]
3434
|> Expect.keyframes
35-
[ "0% { opacity: 1; transform: translateX(50px); }"
36-
, "100% { opacity: 0.5; transform: translateX(100px); }"
35+
[ "0% { opacity: 1; transform: translate3d(50px,0,0); }"
36+
, "100% { opacity: 0.5; transform: translate3d(100px,0,0); }"
3737
]
3838
, test "rendering class properties" <|
3939
\_ ->

tests/TransformTest.elm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ suite =
1616
, Transform.rotate 5
1717
]
1818
|> Transform.toString
19-
|> Expect.equal "translateX(5px) translateY(5px) scale(5,5) rotate(5deg)"
19+
|> Expect.equal "translate3d(5px,5px,0) scale3d(5,5,1) rotate3d(0,0,1,5deg)"
20+
, test "XY transforms are prioritised over separate X and Y transforms" <|
21+
\_ ->
22+
[ Transform.xy 10 15
23+
, Transform.x 2
24+
, Transform.y 4
25+
]
26+
|> Transform.toString
27+
|> Expect.equal "translate3d(10px,15px,0)"
2028
, test "The last property wins if duplicated" <|
2129
\_ ->
2230
[ Transform.rotate 5
@@ -25,5 +33,5 @@ suite =
2533
, Transform.x 10
2634
]
2735
|> Transform.toString
28-
|> Expect.equal "translateX(10px) rotate(12deg)"
36+
|> Expect.equal "translate3d(10px,0,0) rotate3d(0,0,1,12deg)"
2937
]

0 commit comments

Comments
 (0)